Changeset 9335
- Timestamp:
- 01/09/2015 07:54:02 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-messages/bp-messages-classes.php
r9332 r9335 360 360 'limit' => null, 361 361 'page' => null, 362 'search_terms' => '' 362 'search_terms' => '', 363 'meta_query' => array() 363 364 ); 364 365 $r = wp_parse_args( $args, $defaults ); 365 366 366 367 $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = ''; 368 $meta_query_sql = array( 369 'join' => '', 370 'where' => '' 371 ); 367 372 368 373 if ( $r['limit'] && $r['page'] ) { … … 391 396 } 392 397 398 // Process meta query into SQL 399 $meta_query = self::get_meta_query_sql( $r['meta_query'] ); 400 if ( ! empty( $meta_query['join'] ) ) { 401 $meta_query_sql['join'] = $meta_query['join']; 402 } 403 if ( ! empty( $meta_query['where'] ) ) { 404 $meta_query_sql['where'] = $meta_query['where']; 405 } 406 393 407 // set up SQL array 394 408 $sql = array(); 395 409 $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent'; 396 $sql['from'] = "FROM {$bp->messages->table_name_recipients} r INNER JOIN {$bp->messages->table_name_messages} m ON m.thread_id = r.thread_id ";397 $sql['where'] = "WHERE r.is_deleted = 0 {$user_id_sql} {$sender_sql} {$type_sql} {$search_sql} ";410 $sql['from'] = "FROM {$bp->messages->table_name_recipients} r INNER JOIN {$bp->messages->table_name_messages} m ON m.thread_id = r.thread_id {$meta_query_sql['join']}"; 411 $sql['where'] = "WHERE r.is_deleted = 0 {$user_id_sql} {$sender_sql} {$type_sql} {$search_sql} {$meta_query_sql['where']}"; 398 412 $sql['misc'] = "GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}"; 399 413 … … 434 448 */ 435 449 return apply_filters( 'bp_messages_thread_current_threads', array( 'threads' => &$threads, 'total' => (int) $total_threads ) ); 450 } 451 452 /** 453 * Get the SQL for the 'meta_query' param in BP_Messages_Thread::get_current_threads_for_user(). 454 * 455 * We use WP_Meta_Query to do the heavy lifting of parsing the meta_query array 456 * and creating the necessary SQL clauses. 457 * 458 * @since BuddyPress (2.2.0) 459 * 460 * @param array $meta_query An array of meta_query filters. See the 461 * documentation for WP_Meta_Query for details. 462 * @return array $sql_array 'join' and 'where' clauses. 463 */ 464 public static function get_meta_query_sql( $meta_query = array() ) { 465 global $wpdb; 466 467 $sql_array = array( 468 'join' => '', 469 'where' => '', 470 ); 471 472 if ( ! empty( $meta_query ) ) { 473 $meta_query = new WP_Meta_Query( $meta_query ); 474 475 // WP_Meta_Query expects the table name at 476 // $wpdb->messagemeta 477 $wpdb->messagemeta = buddypress()->messages->table_name_meta; 478 479 return $meta_query->get_sql( 'message', 'm', 'id' ); 480 } 481 482 return $sql_array; 436 483 } 437 484 -
trunk/src/bp-messages/bp-messages-template.php
r9315 r9335 382 382 * @type string $page_arg URL argument used for the pagination param. 383 383 * Default: 'mpage'. 384 * @type array $meta_query Meta query arguments. Only applicable if $box is 385 * not 'notices'. See WP_Meta_Query more details. 384 386 * } 385 387 * @return bool True if there are threads to display, otherwise false. … … 406 408 'search_terms' => isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '', 407 409 'page_arg' => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679 410 'meta_query' => array() 408 411 ), 'has_message_threads' ); 409 412 -
trunk/tests/phpunit/testcases/messages/template.php
r9302 r9335 69 69 $this->assertSame( array( $t1 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) ); 70 70 } 71 72 /** 73 * @group bp_has_message_threads 74 * @group meta_query 75 */ 76 public function test_thread_has_messages_meta_query() { 77 $u1 = $this->factory->user->create(); 78 $u2 = $this->factory->user->create(); 79 80 // create some threads 81 $t1 = $this->factory->message->create( array( 82 'sender_id' => $u1, 83 'recipients' => array( $u2 ), 84 'subject' => 'This is a knive', 85 ) ); 86 $t2 = $this->factory->message->create( array( 87 'sender_id' => $u1, 88 'recipients' => array( $u2 ), 89 'subject' => 'Oy', 90 ) ); 91 92 // misc threads 93 $this->factory->message->create_many( 3, array( 94 'sender_id' => $u1, 95 'recipients' => array( $u2 ), 96 ) ); 97 98 // create some replies for thread 1 99 $this->factory->message->create( array( 100 'thread_id' => $t1, 101 'sender_id' => $u2, 102 'recipients' => array( $u1 ), 103 'content' => "That's a spoon", 104 ) ); 105 $this->factory->message->create( array( 106 'thread_id' => $t1, 107 'sender_id' => $u1, 108 'recipients' => array( $u2 ), 109 'content' => "I see you've played knivey-spooney before.", 110 ) ); 111 $this->factory->message->create( array( 112 'thread_id' => $t1, 113 'sender_id' => $u2, 114 'recipients' => array( $u1 ), 115 'content' => '*crickets*', 116 ) ); 117 118 // create some replies for thread 2 119 $this->factory->message->create( array( 120 'thread_id' => $t2, 121 'sender_id' => $u2, 122 'recipients' => array( $u1 ), 123 'content' => "Oy yourself.", 124 ) ); 125 126 // grab the message ids as individual variables for thread 1 127 $thread = new BP_Messages_Thread( $t1 ); 128 $mids = wp_list_pluck( $thread->messages, 'id' ); 129 list( $m1, $m2, $m3, $m4 ) = $mids; 130 131 // grab the message ids as individual variables for thread 2 132 $thread = new BP_Messages_Thread( $t2 ); 133 $mids = wp_list_pluck( $thread->messages, 'id' ); 134 list( $m5, $m6 ) = $mids; 135 136 // add meta for some of the messages 137 bp_messages_update_meta( $m1, 'utensil', 'knive' ); 138 bp_messages_update_meta( $m1, 'is_knive', 'yes' ); 139 bp_messages_update_meta( $m1, "starred_by_user_{$u2}", true ); 140 141 bp_messages_update_meta( $m2, 'utensil', 'spoon' ); 142 bp_messages_update_meta( $m2, 'is_knive', 'no' ); 143 bp_messages_update_meta( $m2, 'is_spoon', 'yes' ); 144 145 bp_messages_update_meta( $m3, "starred_by_user_{$u2}", true ); 146 147 bp_messages_update_meta( $m5, "starred_by_user_{$u2}", true ); 148 149 // now, do the message thread loop query 150 global $messages_template; 151 bp_has_message_threads( array( 152 'user_id' => $u2, 153 'meta_query' => array( 154 array( 155 'key' => "starred_by_user_{$u2}" 156 ), 157 ) 158 ) ); 159 160 $this->assertEquals( 2, $messages_template->thread_count ); 161 $this->assertEqualSets( array( $t1, $t2 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) ); 162 } 163 164 /** 165 * @group bp_has_message_threads 166 * @group meta_query 167 */ 168 public function test_thread_has_messages_meta_query_multiple_clauses_relation_and() { 169 $u1 = $this->factory->user->create(); 170 $u2 = $this->factory->user->create(); 171 172 // create some threads 173 $t1 = $this->factory->message->create( array( 174 'sender_id' => $u1, 175 'recipients' => array( $u2 ), 176 'subject' => 'This is a knive', 177 ) ); 178 $t2 = $this->factory->message->create( array( 179 'sender_id' => $u1, 180 'recipients' => array( $u2 ), 181 'subject' => 'Oy', 182 ) ); 183 184 // misc threads 185 $this->factory->message->create_many( 3, array( 186 'sender_id' => $u1, 187 'recipients' => array( $u2 ), 188 ) ); 189 190 // create some replies for thread 1 191 $this->factory->message->create( array( 192 'thread_id' => $t1, 193 'sender_id' => $u2, 194 'recipients' => array( $u1 ), 195 'content' => "That's a spoon", 196 ) ); 197 $this->factory->message->create( array( 198 'thread_id' => $t1, 199 'sender_id' => $u1, 200 'recipients' => array( $u2 ), 201 'content' => "I see you've played knivey-spooney before.", 202 ) ); 203 $this->factory->message->create( array( 204 'thread_id' => $t1, 205 'sender_id' => $u2, 206 'recipients' => array( $u1 ), 207 'content' => '*crickets*', 208 ) ); 209 210 // create some replies for thread 2 211 $this->factory->message->create( array( 212 'thread_id' => $t2, 213 'sender_id' => $u2, 214 'recipients' => array( $u1 ), 215 'content' => "Oy yourself.", 216 ) ); 217 218 // grab the message ids as individual variables for thread 1 219 $thread = new BP_Messages_Thread( $t1 ); 220 $mids = wp_list_pluck( $thread->messages, 'id' ); 221 list( $m1, $m2, $m3, $m4 ) = $mids; 222 223 // grab the message ids as individual variables for thread 2 224 $thread = new BP_Messages_Thread( $t2 ); 225 $mids = wp_list_pluck( $thread->messages, 'id' ); 226 list( $m5, $m6 ) = $mids; 227 228 // add meta for some of the messages 229 bp_messages_update_meta( $m1, 'utensil', 'knive' ); 230 bp_messages_update_meta( $m1, 'is_knive', 'yes' ); 231 bp_messages_update_meta( $m1, "starred_by_user_{$u2}", true ); 232 233 bp_messages_update_meta( $m2, 'utensil', 'spoon' ); 234 bp_messages_update_meta( $m2, 'is_knive', 'no' ); 235 bp_messages_update_meta( $m2, 'is_spoon', 'yes' ); 236 237 bp_messages_update_meta( $m3, "starred_by_user_{$u2}", true ); 238 239 bp_messages_update_meta( $m5, "starred_by_user_{$u2}", true ); 240 241 // now, do the message thread loop query 242 global $messages_template; 243 bp_has_message_threads( array( 244 'user_id' => $u2, 245 'meta_query' => array( 246 'relation' => 'AND', 247 array( 248 'key' => "starred_by_user_{$u2}" 249 ), 250 array( 251 'key' => 'utensil', 252 'value' => 'knive', 253 ), 254 ) 255 ) ); 256 257 $this->assertEquals( 1, $messages_template->thread_count ); 258 $this->assertEqualSets( array( $t1 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) ); 259 } 71 260 }
Note: See TracChangeset
for help on using the changeset viewer.