[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-messages/classes/ -> class-bp-messages-box-template.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Messages Box Template Class.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MessagesClasses
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  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 BP_Messages_Thread|bool
  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          $function_args = func_get_args();
 111  
 112          // Backward compatibility with old method of passing arguments.
 113          if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
 114              _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__ ) );
 115  
 116              $old_args_keys = array(
 117                  0 => 'user_id',
 118                  1 => 'box',
 119                  2 => 'per_page',
 120                  3 => 'max',
 121                  4 => 'type',
 122                  5 => 'search_terms',
 123                  6 => 'page_arg'
 124              );
 125  
 126              $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 127          }
 128  
 129          $r = bp_parse_args(
 130              $args,
 131              array(
 132                  'page'         => 1,
 133                  'per_page'     => 10,
 134                  'page_arg'     => 'mpage',
 135                  'box'          => 'inbox',
 136                  'type'         => 'all',
 137                  'user_id'      => bp_loggedin_user_id(),
 138                  'max'          => false,
 139                  'search_terms' => '',
 140                  'meta_query'   => array(),
 141              )
 142          );
 143  
 144          $this->pag_arg      = sanitize_key( $r['page_arg'] );
 145          $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 146          $this->pag_num      = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 147          $this->user_id      = $r['user_id'];
 148          $this->box          = $r['box'];
 149          $this->type         = $r['type'];
 150          $this->search_terms = $r['search_terms'];
 151  
 152          if ( 'notices' === $this->box ) {
 153              $this->threads = BP_Messages_Notice::get_notices( array(
 154                  'pag_num'  => $this->pag_num,
 155                  'pag_page' => $this->pag_page
 156              ) );
 157          } else {
 158              $threads = BP_Messages_Thread::get_current_threads_for_user( array(
 159                  'user_id'      => $this->user_id,
 160                  'box'          => $this->box,
 161                  'type'         => $this->type,
 162                  'limit'        => $this->pag_num,
 163                  'page'         => $this->pag_page,
 164                  'search_terms' => $this->search_terms,
 165                  'meta_query'   => $r['meta_query'],
 166              ) );
 167  
 168              $this->threads            = isset( $threads['threads'] ) ? $threads['threads'] : array();
 169              $this->total_thread_count = isset( $threads['total'] ) ? $threads['total'] : 0;
 170          }
 171  
 172          if ( !$this->threads ) {
 173              $this->thread_count       = 0;
 174              $this->total_thread_count = 0;
 175          } else {
 176              $total_notice_count = BP_Messages_Notice::get_total_notice_count();
 177  
 178              if ( empty( $r['max'] ) || ( (int) $r['max'] >= (int) $total_notice_count ) ) {
 179                  if ( 'notices' === $this->box ) {
 180                      $this->total_thread_count = (int) $total_notice_count;
 181                  }
 182              } else {
 183                  $this->total_thread_count = (int) $r['max'];
 184              }
 185  
 186              if ( ! empty( $r['max'] ) ) {
 187                  if ( (int) $r['max'] >= count( $this->threads ) ) {
 188                      $this->thread_count = count( $this->threads );
 189                  } else {
 190                      $this->thread_count = (int) $r['max'];
 191                  }
 192              } else {
 193                  $this->thread_count = count( $this->threads );
 194              }
 195          }
 196  
 197          if ( (int) $this->total_thread_count && (int) $this->pag_num ) {
 198              $pag_args = array(
 199                  $r['page_arg'] => '%#%',
 200              );
 201  
 202              if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
 203                  $base = remove_query_arg( 's', wp_get_referer() );
 204              } else {
 205                  $base = '';
 206              }
 207  
 208              $add_args = array();
 209  
 210              if ( ! empty( $this->search_terms ) ) {
 211                  $add_args['s'] = $this->search_terms;
 212              }
 213  
 214              $this->pag_links = paginate_links( array(
 215                  'base'      => add_query_arg( $pag_args, $base ),
 216                  'format'    => '',
 217                  'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
 218                  'current'   => $this->pag_page,
 219                  'prev_text' => _x( '&larr;', 'Message pagination previous text', 'buddypress' ),
 220                  'next_text' => _x( '&rarr;', 'Message pagination next text', 'buddypress' ),
 221                  'mid_size'  => 1,
 222                  'add_args'  => $add_args,
 223              ) );
 224          }
 225      }
 226  
 227      /**
 228       * Whether there are threads available in the loop.
 229       *
 230       * @see bp_has_message_threads()
 231       *
 232       * @return bool True if there are items in the loop, otherwise false.
 233       */
 234  	public function has_threads() {
 235          return ( $this->thread_count );
 236      }
 237  
 238      /**
 239       * Set up the next member and iterate index.
 240       *
 241       * @return BP_Messages_Thread The next member to iterate over.
 242       */
 243  	public function next_thread() {
 244          $this->current_thread++;
 245          $this->thread = $this->threads[ $this->current_thread ];
 246  
 247          return $this->thread;
 248      }
 249  
 250      /**
 251       * Rewind the threads and reset thread index.
 252       */
 253  	public function rewind_threads() {
 254          $this->current_thread = -1;
 255          if ( $this->thread_count > 0 ) {
 256              $this->thread = $this->threads[0];
 257          }
 258      }
 259  
 260      /**
 261       * Whether there are threads left in the loop to iterate over.
 262       *
 263       * This method is used by {@link bp_message_threads()} as part of the
 264       * while loop that controls iteration inside the threads loop, eg:
 265       *     while ( bp_message_threads() ) { ...
 266       *
 267       * @see bp_message_threads()
 268       *
 269       * @return bool True if there are more threads to show, otherwise false.
 270       */
 271  	public function message_threads() {
 272          if ( $this->current_thread + 1 < $this->thread_count ) {
 273              return true;
 274          } elseif ( $this->current_thread + 1 === $this->thread_count ) {
 275  
 276              /**
 277               * Fires when at the end of threads to iterate over.
 278               *
 279               * @since 1.5.0
 280               */
 281              do_action( 'messages_box_loop_end' );
 282              // Do some cleaning up after the loop.
 283              $this->rewind_threads();
 284          }
 285  
 286          $this->in_the_loop = false;
 287          return false;
 288      }
 289  
 290      /**
 291       * Set up the current thread inside the loop.
 292       *
 293       * Used by {@link bp_message_thread()} to set up the current thread data
 294       * while looping, so that template tags used during that iteration make
 295       * reference to the current thread.
 296       *
 297       * @see bp_message_thread()
 298       */
 299  	public function the_message_thread() {
 300  
 301          $this->in_the_loop = true;
 302          $this->thread      = $this->next_thread();
 303  
 304          if ( ! bp_is_current_action( 'notices' ) ) {
 305              $last_message_index     = count( $this->thread->messages ) - 1;
 306              $this->thread->messages = array_reverse( (array) $this->thread->messages );
 307  
 308              // Set up the last message data.
 309              if ( count( $this->thread->messages ) > 1 ) {
 310                  if ( 'inbox' == $this->box ) {
 311                      foreach ( (array) $this->thread->messages as $key => $message ) {
 312                          if ( bp_loggedin_user_id() != $message->sender_id ) {
 313                              $last_message_index = $key;
 314                              break;
 315                          }
 316                      }
 317  
 318                  } elseif ( 'sentbox' == $this->box ) {
 319                      foreach ( (array) $this->thread->messages as $key => $message ) {
 320                          if ( bp_loggedin_user_id() == $message->sender_id ) {
 321                              $last_message_index = $key;
 322                              break;
 323                          }
 324                      }
 325                  }
 326              }
 327  
 328              $this->thread->last_message_id      = $this->thread->messages[ $last_message_index ]->id;
 329              $this->thread->last_message_date    = $this->thread->messages[ $last_message_index ]->date_sent;
 330              $this->thread->last_sender_id       = $this->thread->messages[ $last_message_index ]->sender_id;
 331              $this->thread->last_message_subject = $this->thread->messages[ $last_message_index ]->subject;
 332              $this->thread->last_message_content = $this->thread->messages[ $last_message_index ]->message;
 333          }
 334  
 335          // Loop has just started.
 336          if ( 0 === $this->current_thread ) {
 337  
 338              /**
 339               * Fires if at the start of the message thread loop.
 340               *
 341               * @since 1.5.0
 342               */
 343              do_action( 'messages_box_loop_start' );
 344          }
 345      }
 346  }


Generated: Tue Sep 21 01:01:37 2021 Cross-referenced by PHPXref 0.7.1