Skip to content

Commit 01dfcd5

Browse files
authored
Merge pull request #57 from Upstatement/56-ajax-search-filter
#56 Apply filters to 'Add Post' search results
2 parents 10100fc + 9fe9649 commit 01dfcd5

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

assets/js/script.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ jQuery(function($) {
564564

565565
var request = {
566566
action: 'sm_search',
567-
query: that.search_query
567+
query: that.search_query,
568+
stream_id: $('#post_ID').val()
568569
};
569570

570571
$.post(ajaxurl, request, function(results) {

includes/class-stream-manager-admin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ public function ajax_retrieve_posts( $request ) {
420420
* @param array $request AJAX request (uses $_POST instead)
421421
*/
422422
public function ajax_search_posts( $request ) {
423-
if ( !isset( $_POST['query'] ) ) $this->ajax_respond( 'error' );
424-
$output = StreamManagerAjaxHelper::search_posts($_POST['query']);
423+
if ( !isset( $_POST['query'] ) || !isset( $_POST['stream_id'] ) ) $this->ajax_respond( 'error' );
424+
$output = StreamManagerAjaxHelper::search_posts($_POST['query'], $_POST['stream_id']);
425425

426426
$this->ajax_respond( 'success', $output );
427427
}

includes/class-stream-manager-ajax-helper.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,21 @@ public static function retrieve_posts($queue) {
4848
* @since 1.0.0
4949
*
5050
* @param string $query search term
51+
* @param int $stream_id post id of current stream
5152
*
5253
* @return array $output posts w/ ids, date, title, human time diff
5354
*/
54-
public static function search_posts($query) {
55-
$posts = Timber::get_posts(array(
55+
public static function search_posts( $query, $stream_id ) {
56+
$defaults = array(
5657
's' => $query,
5758
'post_type' => 'post',
5859
'post_status' => 'publish',
5960
'posts_per_page' => 10
60-
));
61+
);
62+
$stream = new TimberStream($stream_id);
63+
$args = array_merge( $defaults, $stream->get( 'query' ) );
64+
65+
$posts = Timber::get_posts( $args );
6166

6267
$output = array();
6368

tests/StreamManager_UnitTestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ public function buildPosts($count = 10) {
1111
return $post_ids;
1212
}
1313

14-
public function buildStream() {
15-
$pid = $this->factory->post->create(array('post_type' => 'sm_stream', 'post_content' => '', 'post_title' => 'Sample Stream'));
14+
function buildStream( $name = 'Sample Stream', $options = array() ) {
15+
$pid = $this->factory->post->create(array('post_type' => 'sm_stream', 'post_content' => '', 'post_title' => $name));
16+
add_filter('stream-manager/options/id='.$pid, function($defaults, $stream) use ($options) {
17+
$defaults['query'] = array_merge($defaults['query'], $options);
18+
return $defaults;
19+
}, 10, 2);
1620
$stream = new TimberStream($pid);
1721
return $stream;
1822
}

tests/test-stream-manager-ajax-helper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,29 @@ function testRetrievePostsCheckTitle() {
3030
}
3131

3232
function testSearchPosts() {
33+
$pid = $this->buildStream();
3334
$bagel_post = $this->factory->post->create(array('post_title' => 'Bagels'));
3435
$croissant_post = $this->factory->post->create(array('post_title' => 'Croissants'));
3536
$pastries_post = $this->factory->post->create(array('post_title' => 'Croissants and Bagels'));
36-
$output = StreamManagerAjaxHelper::search_posts('bagel');
37+
$output = StreamManagerAjaxHelper::search_posts('bagel', $pid);
3738
$this->assertEquals(2, count($output));
3839
$this->assertEquals('1 min', $output[0]['human_date']);
3940
}
4041

42+
function testSearchPostsAppliesFilter() {
43+
$pid = $this->buildStream('Test Stream', array('post_type' => 'pastry'));
44+
$this->factory->post->create(array('post_title' => 'bagel1'));
45+
$this->factory->post->create(array('post_title' => 'bagel2'));
46+
$this->factory->post->create(array('post_title' => 'bagel3', 'post_type' => 'pastry'));
47+
$output = StreamManagerAjaxHelper::search_posts('bagel', $pid);
48+
$this->assertEquals(1, count($output));
49+
}
50+
4151
function testSearchPostsNoMatches() {
52+
$pid = $this->buildStream();
4253
$bagel_post = $this->factory->post->create(array('post_title' => 'Bagels'));
43-
$output = StreamManagerAjaxHelper::search_posts('muffin');
54+
$output = StreamManagerAjaxHelper::search_posts('muffin', $pid);
4455
$this->assertEquals(0, count($output));
4556
}
4657

47-
4858
}

tests/test-stream-manager-hooks.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
class TestStreamManagerHooks extends StreamManager_UnitTestCase {
44

5-
function buildStream( $name = 'Sample Stream', $options = array() ) {
6-
$pid = $this->factory->post->create(array('post_type' => 'sm_stream', 'post_content' => '', 'post_title' => $name));
7-
add_filter('stream-manager/options/id='.$pid, function($defaults, $stream) use ($options) {
8-
$defaults['query'] = array_merge($defaults['query'], $options);
9-
return $defaults;
10-
}, 10, 2);
11-
$stream = new TimberStream($pid);
12-
return $stream;
13-
}
14-
155
function testQueryHook() {
166
$stream = $this->buildStream('Sample Stream', array('post_type' => 'article'));
177
$this->buildPosts(5);

0 commit comments

Comments
 (0)