Skip to content

Commit 5594168

Browse files
authored
Merge pull request #93 from brainstormforce/hidden-comments
Breaking: Show comments for a unlisted posts
2 parents ad685ea + 29fc28a commit 5594168

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

Diff for: class-unlist-posts.php

-32
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public function init() {
6464
add_action( 'wp_head', array( $this, 'hide_post_from_searchengines' ) );
6565
add_filter( 'wp_robots', array( $this, 'no_robots_for_unlisted_posts' ) );
6666
add_filter( 'rank_math/frontend/robots', array( $this, 'change_robots_for_rankmath' ) );
67-
add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 20, 2 );
6867
add_filter( 'wp_list_pages_excludes', array( $this, 'wp_list_pages_excludes' ) );
6968
}
7069

@@ -205,37 +204,6 @@ public function no_robots_for_unlisted_posts( $robots ) {
205204
return $robots;
206205
}
207206

208-
/**
209-
* Filter where clause to hide selected posts.
210-
*
211-
* @since 1.0.1
212-
*
213-
* @param Array $clauses Comment Query Clauses.
214-
* @param WP_Query $query WP_Query &$this The WP_Query instance (passed by reference).
215-
*
216-
* @return String $where Where clause.
217-
*/
218-
public function comments_clauses( $clauses, $query ) {
219-
220-
// Bail if posts unlists is disabled.
221-
if ( false === $this->allow_post_unlist() ) {
222-
return $clauses;
223-
}
224-
225-
$hidden_posts = get_option( 'unlist_posts', array() );
226-
227-
// bail if none of the posts are hidden or we are on admin page or singular page.
228-
if ( ( is_admin() && ! wp_doing_ajax() ) || empty( $hidden_posts ) ) {
229-
return $clauses;
230-
}
231-
232-
$where = $clauses['where'];
233-
$where .= ' AND comment_post_ID NOT IN ( ' . esc_sql( $this->hidden_post_string() ) . ' ) ';
234-
$clauses['where'] = $where;
235-
236-
return $clauses;
237-
}
238-
239207
/**
240208
* Exclude the unlisted posts from the wp_list_posts()
241209
*

Diff for: tests/test-comments-unlisted-post.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Class CommentsUnlistedPost
4+
*
5+
* @package Unlist_Posts
6+
*/
7+
8+
/**
9+
* Make sure unlisted posts are hidden from get_adjacent_post().
10+
*/
11+
class CommentsUnlistedPost extends WP_UnitTestCase {
12+
/**
13+
* Test that posts of an unlisted comment are shown.
14+
*/
15+
public function test_comments_unlisted_post() {
16+
$editor_user_id = self::factory()->user->create(
17+
array(
18+
'role' => 'editor',
19+
)
20+
);
21+
22+
wp_set_current_user( $editor_user_id );
23+
24+
// Create an listed post.
25+
$listed_post = self::factory()->post->create();
26+
27+
// Add comments for the post.
28+
$comment_ids = array();
29+
for ( $i = 0; $i < 5; $i++ ) {
30+
$comment_ids[] = self::factory()->comment->create(
31+
array(
32+
'comment_post_ID' => $listed_post,
33+
)
34+
);
35+
}
36+
37+
// Set the post as unlisted.
38+
$_POST['unlist_posts'] = true;
39+
$_POST['unlist_post_nounce'] = wp_create_nonce( 'unlist_post_nounce' );
40+
Unlist_Posts_Admin::instance()->save_meta( $listed_post );
41+
42+
// Get comments for the post.
43+
$comments = get_comments(
44+
array(
45+
'post_id' => $listed_post,
46+
)
47+
);
48+
49+
$this->assertCount( 5, $comments );
50+
}
51+
}

0 commit comments

Comments
 (0)