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


Generated: Sat Apr 20 01:00:58 2024 Cross-referenced by PHPXref 0.7.1