Changeset 10522 for trunk/src/bp-messages/bp-messages-template.php
- Timestamp:
- 02/05/2016 05:22:55 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/bp-messages-template.php
r10421 r10522 11 11 defined( 'ABSPATH' ) || exit; 12 12 13 /** 14 * Message Box Template Class 15 */ 16 class BP_Messages_Box_Template { 17 18 /** 19 * The loop iterator. 20 * 21 * @var int 22 */ 23 public $current_thread = -1; 24 25 /** 26 * The number of threads returned by the paged query. 27 * 28 * @var int 29 */ 30 public $current_thread_count = 0; 31 32 /** 33 * Total number of threads matching the query params. 34 * 35 * @var int 36 */ 37 public $total_thread_count = 0; 38 39 /** 40 * Array of threads located by the query. 41 * 42 * @var array 43 */ 44 public $threads = array(); 45 46 /** 47 * The thread object currently being iterated on. 48 * 49 * @var object 50 */ 51 public $thread = false; 52 53 /** 54 * A flag for whether the loop is currently being iterated. 55 * 56 * @var bool 57 */ 58 public $in_the_loop = false; 59 60 /** 61 * User ID of the current inbox. 62 * 63 * @var int 64 */ 65 public $user_id = 0; 66 67 /** 68 * The current "box" view ('notices', 'sentbox', 'inbox'). 69 * 70 * @var string 71 */ 72 public $box = 'inbox'; 73 74 /** 75 * The page number being requested. 76 * 77 * @var int 78 */ 79 public $pag_page = 1; 80 81 /** 82 * The number of items being requested per page. 83 * 84 * @var int 85 */ 86 public $pag_num = 10; 87 88 /** 89 * An HTML string containing pagination links. 90 * 91 * @var string 92 */ 93 public $pag_links = ''; 94 95 /** 96 * Search terms for limiting the thread query. 97 * 98 * @var string 99 */ 100 public $search_terms = ''; 101 102 /** 103 * Constructor method. 104 * 105 * @param array $args { 106 * Array of arguments. See bp_has_message_threads() for full description. 107 * } 108 */ 109 public function __construct( $args = array() ) { 110 111 // Backward compatibility with old method of passing arguments. 112 if ( ! is_array( $args ) || func_num_args() > 1 ) { 113 _deprecated_argument( __METHOD__, '2.2.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 114 115 $old_args_keys = array( 116 0 => 'user_id', 117 1 => 'box', 118 2 => 'per_page', 119 3 => 'max', 120 4 => 'type', 121 5 => 'search_terms', 122 6 => 'page_arg' 123 ); 124 125 $func_args = func_get_args(); 126 $args = bp_core_parse_args_array( $old_args_keys, $func_args ); 127 } 128 129 $r = wp_parse_args( $args, array( 130 'page' => 1, 131 'per_page' => 10, 132 'page_arg' => 'mpage', 133 'box' => 'inbox', 134 'type' => 'all', 135 'user_id' => bp_loggedin_user_id(), 136 'max' => false, 137 'search_terms' => '', 138 'meta_query' => array(), 139 ) ); 140 141 $this->pag_arg = sanitize_key( $r['page_arg'] ); 142 $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] ); 143 $this->pag_num = bp_sanitize_pagination_arg( 'num', $r['per_page'] ); 144 $this->user_id = $r['user_id']; 145 $this->box = $r['box']; 146 $this->type = $r['type']; 147 $this->search_terms = $r['search_terms']; 148 149 if ( 'notices' === $this->box ) { 150 $this->threads = BP_Messages_Notice::get_notices( array( 151 'pag_num' => $this->pag_num, 152 'pag_page' => $this->pag_page 153 ) ); 154 } else { 155 $threads = BP_Messages_Thread::get_current_threads_for_user( array( 156 'user_id' => $this->user_id, 157 'box' => $this->box, 158 'type' => $this->type, 159 'limit' => $this->pag_num, 160 'page' => $this->pag_page, 161 'search_terms' => $this->search_terms, 162 'meta_query' => $r['meta_query'], 163 ) ); 164 165 $this->threads = $threads['threads']; 166 $this->total_thread_count = $threads['total']; 167 } 168 169 if ( !$this->threads ) { 170 $this->thread_count = 0; 171 $this->total_thread_count = 0; 172 } else { 173 $total_notice_count = BP_Messages_Notice::get_total_notice_count(); 174 175 if ( empty( $r['max'] ) || ( (int) $r['max'] >= (int) $total_notice_count ) ) { 176 if ( 'notices' === $this->box ) { 177 $this->total_thread_count = (int) $total_notice_count; 178 } 179 } else { 180 $this->total_thread_count = (int) $r['max']; 181 } 182 183 if ( ! empty( $r['max'] ) ) { 184 if ( (int) $r['max'] >= count( $this->threads ) ) { 185 $this->thread_count = count( $this->threads ); 186 } else { 187 $this->thread_count = (int) $r['max']; 188 } 189 } else { 190 $this->thread_count = count( $this->threads ); 191 } 192 } 193 194 if ( (int) $this->total_thread_count && (int) $this->pag_num ) { 195 $pag_args = array( 196 $r['page_arg'] => '%#%', 197 ); 198 199 if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 200 $base = remove_query_arg( 's', wp_get_referer() ); 201 } else { 202 $base = ''; 203 } 204 205 $add_args = array(); 206 207 if ( ! empty( $this->search_terms ) ) { 208 $add_args['s'] = $this->search_terms; 209 } 210 211 $this->pag_links = paginate_links( array( 212 'base' => add_query_arg( $pag_args, $base ), 213 'format' => '', 214 'total' => ceil( (int) $this->total_thread_count / (int) $this->pag_num ), 215 'current' => $this->pag_page, 216 'prev_text' => _x( '←', 'Message pagination previous text', 'buddypress' ), 217 'next_text' => _x( '→', 'Message pagination next text', 'buddypress' ), 218 'mid_size' => 1, 219 'add_args' => $add_args, 220 ) ); 221 } 222 } 223 224 /** 225 * Whether there are threads available in the loop. 226 * 227 * @see bp_has_message_threads() 228 * 229 * @return bool True if there are items in the loop, otherwise false. 230 */ 231 public function has_threads() { 232 if ( $this->thread_count ) { 233 return true; 234 } 235 236 return false; 237 } 238 239 /** 240 * Set up the next member and iterate index. 241 * 242 * @return object The next member to iterate over. 243 */ 244 public function next_thread() { 245 $this->current_thread++; 246 $this->thread = $this->threads[$this->current_thread]; 247 248 return $this->thread; 249 } 250 251 /** 252 * Rewind the threads and reset thread index. 253 */ 254 public function rewind_threads() { 255 $this->current_thread = -1; 256 if ( $this->thread_count > 0 ) { 257 $this->thread = $this->threads[0]; 258 } 259 } 260 261 /** 262 * Whether there are threads left in the loop to iterate over. 263 * 264 * This method is used by {@link bp_message_threads()} as part of the 265 * while loop that controls iteration inside the threads loop, eg: 266 * while ( bp_message_threads() ) { ... 267 * 268 * @see bp_message_threads() 269 * 270 * @return bool True if there are more threads to show, otherwise false. 271 */ 272 function message_threads() { 273 if ( $this->current_thread + 1 < $this->thread_count ) { 274 return true; 275 } elseif ( $this->current_thread + 1 == $this->thread_count ) { 276 277 /** 278 * Fires when at the end of threads to iterate over. 279 * 280 * @since 1.5.0 281 */ 282 do_action( 'messages_box_loop_end' ); 283 // Do some cleaning up after the loop. 284 $this->rewind_threads(); 285 } 286 287 $this->in_the_loop = false; 288 return false; 289 } 290 291 /** 292 * Set up the current thread inside the loop. 293 * 294 * Used by {@link bp_message_thread()} to set up the current thread data 295 * while looping, so that template tags used during that iteration make 296 * reference to the current thread. 297 * 298 * @see bp_message_thread() 299 */ 300 public function the_message_thread() { 301 302 $this->in_the_loop = true; 303 $this->thread = $this->next_thread(); 304 305 if ( ! bp_is_current_action( 'notices' ) ) { 306 $last_message_index = count( $this->thread->messages ) - 1; 307 $this->thread->messages = array_reverse( (array) $this->thread->messages ); 308 309 // Set up the last message data. 310 if ( count($this->thread->messages) > 1 ) { 311 if ( 'inbox' == $this->box ) { 312 foreach ( (array) $this->thread->messages as $key => $message ) { 313 if ( bp_loggedin_user_id() != $message->sender_id ) { 314 $last_message_index = $key; 315 break; 316 } 317 } 318 319 } elseif ( 'sentbox' == $this->box ) { 320 foreach ( (array) $this->thread->messages as $key => $message ) { 321 if ( bp_loggedin_user_id() == $message->sender_id ) { 322 $last_message_index = $key; 323 break; 324 } 325 } 326 } 327 } 328 329 $this->thread->last_message_id = $this->thread->messages[ $last_message_index ]->id; 330 $this->thread->last_message_date = $this->thread->messages[ $last_message_index ]->date_sent; 331 $this->thread->last_sender_id = $this->thread->messages[ $last_message_index ]->sender_id; 332 $this->thread->last_message_subject = $this->thread->messages[ $last_message_index ]->subject; 333 $this->thread->last_message_content = $this->thread->messages[ $last_message_index ]->message; 334 } 335 336 // Loop has just started. 337 if ( 0 == $this->current_thread ) { 338 339 /** 340 * Fires if at the start of the message thread loop. 341 * 342 * @since 1.5.0 343 */ 344 do_action( 'messages_box_loop_start' ); 345 } 346 } 347 } 13 require dirname( __FILE__ ) . '/classes/class-bp-messages-box-template.php'; 14 require dirname( __FILE__ ) . '/classes/class-bp-messages-thread-template.php'; 348 15 349 16 /** … … 1753 1420 1754 1421 /** 1755 * Message Thread Template Class1756 */1757 class BP_Messages_Thread_Template {1758 1759 /**1760 * The loop iterator.1761 *1762 * @var int1763 */1764 public $current_message = -1;1765 1766 /**1767 * Number of messages returned by the paged query.1768 *1769 * @var int1770 */1771 public $message_count = 0;1772 1773 /**1774 * The message object currently being iterated on.1775 *1776 * @var object1777 */1778 public $message;1779 1780 /**1781 * Thread that the current messages belong to.1782 *1783 * @var BP_Messages_Thread1784 */1785 public $thread;1786 1787 /**1788 * A flag for whether the loop is currently being iterated.1789 *1790 * @var bool1791 */1792 public $in_the_loop = false;1793 1794 /**1795 * The page number being requested.1796 *1797 * @var int1798 */1799 public $pag_page = 1;1800 1801 /**1802 * The number of items being requested per page.1803 *1804 * @var int1805 */1806 public $pag_num = 10;1807 1808 /**1809 * An HTML string containing pagination links.1810 *1811 * @var string1812 */1813 public $pag_links = '';1814 1815 /**1816 * The total number of messages matching the query.1817 *1818 * @var int1819 */1820 public $total_message_count = 0;1821 1822 /**1823 * Constructor method.1824 *1825 * @see BP_Messages_Thread::populate() for full parameter info.1826 *1827 * @param int $thread_id ID of the message thread to display.1828 * @param string $order Order to show the thread's messages in.1829 * @param array $args Array of arguments for the query.1830 */1831 public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) {1832 $this->thread = new BP_Messages_Thread( $thread_id, $order, $args );1833 $this->message_count = count( $this->thread->messages );1834 }1835 1836 /**1837 * Whether there are messages available in the loop.1838 *1839 * @see bp_thread_has_messages()1840 *1841 * @return bool True if there are items in the loop, otherwise false.1842 */1843 public function has_messages() {1844 if ( ! empty( $this->message_count ) ) {1845 return true;1846 }1847 1848 return false;1849 }1850 1851 /**1852 * Set up the next member and iterate index.1853 *1854 * @return object The next member to iterate over.1855 */1856 public function next_message() {1857 $this->current_message++;1858 $this->message = $this->thread->messages[ $this->current_message ];1859 1860 return $this->message;1861 }1862 1863 /**1864 * Rewind the messages and reset message index.1865 */1866 public function rewind_messages() {1867 $this->current_message = -1;1868 if ( $this->message_count > 0 ) {1869 $this->message = $this->thread->messages[0];1870 }1871 }1872 1873 /**1874 * Whether there are messages left in the loop to iterate over.1875 *1876 * This method is used by {@link bp_thread_messages()} as part of the1877 * while loop that controls iteration inside the messages loop, eg:1878 * while ( bp_thread_messages() ) { ...1879 *1880 * @see bp_thread_messages()1881 *1882 * @return bool True if there are more messages to show, otherwise false.1883 */1884 public function messages() {1885 if ( ( $this->current_message + 1 ) < $this->message_count ) {1886 return true;1887 } elseif ( ( $this->current_message + 1 ) === $this->message_count ) {1888 1889 /**1890 * Fires when at the end of messages to iterate over.1891 *1892 * @since 1.1.01893 */1894 do_action( 'thread_loop_end' );1895 // Do some cleaning up after the loop.1896 $this->rewind_messages();1897 }1898 1899 $this->in_the_loop = false;1900 return false;1901 }1902 1903 /**1904 * Set up the current message inside the loop.1905 *1906 * Used by {@link bp_thread_the_message()} to set up the current1907 * message data while looping, so that template tags used during1908 * that iteration make reference to the current message.1909 *1910 * @see bp_thread_the_message()1911 */1912 public function the_message() {1913 $this->in_the_loop = true;1914 $this->message = $this->next_message();1915 1916 // Loop has just started.1917 if ( 0 === $this->current_message ) {1918 1919 /**1920 * Fires if at the start of the message loop.1921 *1922 * @since 1.1.01923 */1924 do_action( 'thread_loop_start' );1925 }1926 }1927 }1928 1929 /**1930 1422 * Initialize the messages template loop for a specific thread. 1931 1423 *
Note: See TracChangeset
for help on using the changeset viewer.