Esempio n. 1
0
 /**
  * @group bp_messages_update_meta_cache
  */
 public function test_bp_messages_update_meta_cache()
 {
     $u1 = $this->factory->user->create();
     $u2 = $this->factory->user->create();
     // create the thread
     $t1 = $this->factory->message->create(array('sender_id' => $u1, 'recipients' => array($u2), 'subject' => 'This is a knive'));
     // create a reply
     $this->factory->message->create(array('thread_id' => $t1, 'sender_id' => $u2, 'recipients' => array($u1), 'content' => "That's a spoon"));
     // grab the message ids as individual variables
     list($m1, $m2) = $this->get_message_ids($t1);
     // add cache for each message
     bp_messages_update_meta($m1, 'utensil', 'knive');
     bp_messages_update_meta($m1, 'is_knive', 'yes');
     bp_messages_update_meta($m2, 'utensil', 'spoon');
     bp_messages_update_meta($m2, 'is_knive', 'no');
     bp_messages_update_meta($m2, 'is_spoon', 'yes');
     // prime cache
     bp_messages_get_meta($m1, 'utensil');
     // Ensure an empty cache for second message
     wp_cache_delete($m2, 'message_meta');
     // update message meta cache
     bp_messages_update_meta_cache(array($m1, $m2));
     $expected = array($m1 => array('utensil' => array('knive'), 'is_knive' => array('yes')), $m2 => array('utensil' => array('spoon'), 'is_knive' => array('no'), 'is_spoon' => array('yes')));
     $found = array($m1 => wp_cache_get($m1, 'message_meta'), $m2 => wp_cache_get($m2, 'message_meta'));
     $this->assertEquals($expected, $found);
 }
 /**
  * Populate method.
  *
  * Used in constructor.
  *
  * @since 1.0.0
  *
  * @param int    $thread_id The message thread ID.
  * @param string $order     The order to sort the messages. Either 'ASC' or 'DESC'.
  * @param array $args {
  *     Array of arguments.
  *     @type bool $update_meta_cache Whether to pre-fetch metadata for
  *                                   queried message items. Default: true.
  * }
  * @return bool False on failure.
  */
 public function populate($thread_id = 0, $order = 'ASC', $args = array())
 {
     if ('ASC' !== $order && 'DESC' !== $order) {
         $order = 'ASC';
     }
     // merge $args with our defaults
     $r = wp_parse_args($args, array('user_id' => bp_loggedin_user_id(), 'update_meta_cache' => true));
     $this->messages_order = $order;
     $this->thread_id = (int) $thread_id;
     // get messages for thread
     $this->messages = self::get_messages($this->thread_id);
     if (empty($this->messages) || is_wp_error($this->messages)) {
         return false;
     }
     // flip if order is DESC
     if ('DESC' === $order) {
         $this->messages = array_reverse($this->messages);
     }
     $last_message_index = count($this->messages) - 1;
     $this->last_message_id = $this->messages[$last_message_index]->id;
     $this->last_message_date = $this->messages[$last_message_index]->date_sent;
     $this->last_sender_id = $this->messages[$last_message_index]->sender_id;
     $this->last_message_subject = $this->messages[$last_message_index]->subject;
     $this->last_message_content = $this->messages[$last_message_index]->message;
     foreach ((array) $this->messages as $key => $message) {
         $this->sender_ids[$message->sender_id] = $message->sender_id;
     }
     // Fetch the recipients
     $this->recipients = $this->get_recipients();
     // Get the unread count for the logged in user
     if (isset($this->recipients[$r['user_id']])) {
         $this->unread_count = $this->recipients[$r['user_id']]->unread_count;
     }
     // Grab all message meta
     if (true === (bool) $r['update_meta_cache']) {
         bp_messages_update_meta_cache(wp_list_pluck($this->messages, 'id'));
     }
     /**
      * Fires after a BP_Messages_Thread object has been populated.
      *
      * @since 2.2.0
      *
      * @param BP_Messages_Thread $this Message thread object.
      */
     do_action('bp_messages_thread_post_populate', $this);
 }
 /**
  * Populate method.
  *
  * Used in constructor.
  *
  * @since BuddyPress (1.0.0)
  *
  * @param int $thread_id The message thread ID.
  * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'.
  * @param array $args {
  *     Array of arguments.
  *     @type bool $update_meta_cache Whether to pre-fetch metadata for
  *           queried message items. Default: true.
  * }
  * @return bool False on failure.
  */
 public function populate($thread_id = 0, $order = 'ASC', $args = array())
 {
     global $wpdb, $bp;
     if ('ASC' != $order && 'DESC' != $order) {
         $order = 'ASC';
     }
     // merge $args with our defaults
     $r = wp_parse_args($args, array('update_meta_cache' => true));
     $this->messages_order = $order;
     $this->thread_id = $thread_id;
     if (!($this->messages = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent " . $order, $this->thread_id)))) {
         return false;
     }
     foreach ((array) $this->messages as $key => $message) {
         $this->sender_ids[$message->sender_id] = $message->sender_id;
     }
     // Fetch the recipients
     $this->recipients = $this->get_recipients();
     // Get the unread count for the logged in user
     if (isset($this->recipients[bp_loggedin_user_id()])) {
         $this->unread_count = $this->recipients[bp_loggedin_user_id()]->unread_count;
     }
     // Grab all message meta
     if (true === (bool) $r['update_meta_cache']) {
         bp_messages_update_meta_cache(wp_list_pluck($this->messages, 'id'));
     }
     /**
      * Fires after a BP_Messages_Thread object has been populated.
      *
      * @since BuddyPress (2.2.0)
      *
      * @param BP_Messages_Thread Message thread object.
      */
     do_action('bp_messages_thread_post_populate', $this);
 }