[ 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 MessagesTemplate
   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 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              $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
 126          }
 127  
 128          $r = wp_parse_args( $args, array(
 129              'page'         => 1,
 130              'per_page'     => 10,
 131              'page_arg'     => 'mpage',
 132              'box'          => 'inbox',
 133              'type'         => 'all',
 134              'user_id'      => bp_loggedin_user_id(),
 135              'max'          => false,
 136              'search_terms' => '',
 137              'meta_query'   => array(),
 138          ) );
 139  
 140          $this->pag_arg      = sanitize_key( $r['page_arg'] );
 141          $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
 142          $this->pag_num      = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 143          $this->user_id      = $r['user_id'];
 144          $this->box          = $r['box'];
 145          $this->type         = $r['type'];
 146          $this->search_terms = $r['search_terms'];
 147  
 148          if ( 'notices' === $this->box ) {
 149              $this->threads = BP_Messages_Notice::get_notices( array(
 150                  'pag_num'  => $this->pag_num,
 151                  'pag_page' => $this->pag_page
 152              ) );
 153          } else {
 154              $threads = BP_Messages_Thread::get_current_threads_for_user( array(
 155                  'user_id'      => $this->user_id,
 156                  'box'          => $this->box,
 157                  'type'         => $this->type,
 158                  'limit'        => $this->pag_num,
 159                  'page'         => $this->pag_page,
 160                  'search_terms' => $this->search_terms,
 161                  'meta_query'   => $r['meta_query'],
 162              ) );
 163  
 164              $this->threads            = $threads['threads'];
 165              $this->total_thread_count = $threads['total'];
 166          }
 167  
 168          if ( !$this->threads ) {
 169              $this->thread_count       = 0;
 170              $this->total_thread_count = 0;
 171          } else {
 172              $total_notice_count = BP_Messages_Notice::get_total_notice_count();
 173  
 174              if ( empty( $r['max'] ) || ( (int) $r['max'] >= (int) $total_notice_count ) ) {
 175                  if ( 'notices' === $this->box ) {
 176                      $this->total_thread_count = (int) $total_notice_count;
 177                  }
 178              } else {
 179                  $this->total_thread_count = (int) $r['max'];
 180              }
 181  
 182              if ( ! empty( $r['max'] ) ) {
 183                  if ( (int) $r['max'] >= count( $this->threads ) ) {
 184                      $this->thread_count = count( $this->threads );
 185                  } else {
 186                      $this->thread_count = (int) $r['max'];
 187                  }
 188              } else {
 189                  $this->thread_count = count( $this->threads );
 190              }
 191          }
 192  
 193          if ( (int) $this->total_thread_count && (int) $this->pag_num ) {
 194              $pag_args = array(
 195                  $r['page_arg'] => '%#%',
 196              );
 197  
 198              if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
 199                  $base = remove_query_arg( 's', wp_get_referer() );
 200              } else {
 201                  $base = '';
 202              }
 203  
 204              $add_args = array();
 205  
 206              if ( ! empty( $this->search_terms ) ) {
 207                  $add_args['s'] = $this->search_terms;
 208              }
 209  
 210              $this->pag_links = paginate_links( array(
 211                  'base'      => add_query_arg( $pag_args, $base ),
 212                  'format'    => '',
 213                  'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
 214                  'current'   => $this->pag_page,
 215                  'prev_text' => _x( '&larr;', 'Message pagination previous text', 'buddypress' ),
 216                  'next_text' => _x( '&rarr;', 'Message pagination next text', 'buddypress' ),
 217                  'mid_size'  => 1,
 218                  'add_args'  => $add_args,
 219              ) );
 220          }
 221      }
 222  
 223      /**
 224       * Whether there are threads available in the loop.
 225       *
 226       * @see bp_has_message_threads()
 227       *
 228       * @return bool True if there are items in the loop, otherwise false.
 229       */
 230  	public function has_threads() {
 231          if ( $this->thread_count ) {
 232              return true;
 233          }
 234  
 235          return false;
 236      }
 237  
 238      /**
 239       * Set up the next member and iterate index.
 240       *
 241       * @return object 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  	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: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1