[ 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          $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 = 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( '&larr;', 'Message pagination previous text', 'buddypress' ),
 217                  'next_text' => _x( '&rarr;', '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  }


Generated: Sun Nov 17 01:01:36 2019 Cross-referenced by PHPXref 0.7.1