Skip to content

Commit d8b8206

Browse files
Merge pull request #9 from vishalkakadiya/feature/unit-tests
Add filter to enable comment preview on custom post types
2 parents 75c4ffc + e8da54c commit d8b8206

File tree

3 files changed

+110
-69
lines changed

3 files changed

+110
-69
lines changed

clover.xml

+69-68
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,80 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<coverage generated="1632852429">
3-
<project timestamp="1632852429">
2+
<coverage generated="1632933931">
3+
<project timestamp="1632933931">
44
<package name="CommentPreview\Inc">
55
<file name="/var/www/app/inc/classes/class-comment-preview.php">
66
<class name="CommentPreview\Inc\Comment_Preview" namespace="CommentPreview\Inc" fullPackage="CommentPreview" package="CommentPreview">
7-
<metrics complexity="17" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="57" coveredstatements="57" elements="65" coveredelements="65"/>
7+
<metrics complexity="17" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="58" coveredstatements="58" elements="66" coveredelements="66"/>
88
</class>
9-
<line num="20" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="9"/>
10-
<line num="22" type="stmt" count="9"/>
11-
<line num="28" type="method" name="_setup_hooks" visibility="protected" complexity="1" crap="1" count="9"/>
12-
<line num="30" type="stmt" count="9"/>
13-
<line num="32" type="stmt" count="9"/>
14-
<line num="34" type="stmt" count="9"/>
15-
<line num="36" type="stmt" count="9"/>
16-
<line num="38" type="stmt" count="9"/>
17-
<line num="44" type="method" name="enqueue_scripts" visibility="public" complexity="2" crap="2" count="2"/>
18-
<line num="46" type="stmt" count="2"/>
19-
<line num="48" type="stmt" count="1"/>
20-
<line num="49" type="stmt" count="1"/>
21-
<line num="50" type="stmt" count="1"/>
22-
<line num="51" type="stmt" count="1"/>
23-
<line num="52" type="stmt" count="1"/>
24-
<line num="53" type="stmt" count="1"/>
25-
<line num="56" type="stmt" count="1"/>
26-
<line num="57" type="stmt" count="1"/>
27-
<line num="58" type="stmt" count="1"/>
28-
<line num="60" type="stmt" count="1"/>
29-
<line num="61" type="stmt" count="1"/>
30-
<line num="65" type="stmt" count="1"/>
31-
<line num="76" type="method" name="comment_form_fields" visibility="public" complexity="2" crap="2" count="1"/>
32-
<line num="78" type="stmt" count="1"/>
33-
<line num="81" type="stmt" count="1"/>
34-
<line num="84" type="stmt" count="1"/>
35-
<line num="86" type="stmt" count="1"/>
9+
<line num="20" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="10"/>
10+
<line num="22" type="stmt" count="10"/>
11+
<line num="28" type="method" name="_setup_hooks" visibility="protected" complexity="1" crap="1" count="10"/>
12+
<line num="30" type="stmt" count="10"/>
13+
<line num="32" type="stmt" count="10"/>
14+
<line num="34" type="stmt" count="10"/>
15+
<line num="36" type="stmt" count="10"/>
16+
<line num="38" type="stmt" count="10"/>
17+
<line num="44" type="method" name="enqueue_scripts" visibility="public" complexity="2" crap="2" count="3"/>
18+
<line num="51" type="stmt" count="3"/>
19+
<line num="53" type="stmt" count="3"/>
20+
<line num="55" type="stmt" count="2"/>
21+
<line num="56" type="stmt" count="2"/>
22+
<line num="57" type="stmt" count="2"/>
23+
<line num="58" type="stmt" count="2"/>
24+
<line num="59" type="stmt" count="2"/>
25+
<line num="60" type="stmt" count="2"/>
26+
<line num="63" type="stmt" count="2"/>
27+
<line num="64" type="stmt" count="2"/>
28+
<line num="65" type="stmt" count="2"/>
29+
<line num="67" type="stmt" count="2"/>
30+
<line num="68" type="stmt" count="2"/>
31+
<line num="72" type="stmt" count="2"/>
32+
<line num="83" type="method" name="comment_form_fields" visibility="public" complexity="2" crap="2" count="1"/>
33+
<line num="85" type="stmt" count="1"/>
3634
<line num="88" type="stmt" count="1"/>
37-
<line num="90" type="stmt" count="1"/>
35+
<line num="91" type="stmt" count="1"/>
3836
<line num="93" type="stmt" count="1"/>
39-
<line num="104" type="method" name="append_markdown_option" visibility="public" complexity="1" crap="1" count="1"/>
40-
<line num="106" type="stmt" count="1"/>
41-
<line num="109" type="stmt" count="1"/>
42-
<line num="112" type="stmt" count="1"/>
43-
<line num="114" type="stmt" count="1"/>
44-
<line num="124" type="method" name="append_preview_button" visibility="public" complexity="1" crap="1" count="1"/>
45-
<line num="126" type="stmt" count="1"/>
46-
<line num="127" type="stmt" count="1"/>
47-
<line num="128" type="stmt" count="1"/>
48-
<line num="131" type="stmt" count="1"/>
49-
<line num="137" type="method" name="register_rest_route" visibility="public" complexity="1" crap="1" count="9"/>
50-
<line num="139" type="stmt" count="9"/>
51-
<line num="140" type="stmt" count="9"/>
52-
<line num="141" type="stmt" count="9"/>
53-
<line num="143" type="stmt" count="9"/>
54-
<line num="144" type="stmt" count="9"/>
55-
<line num="145" type="stmt" count="9"/>
56-
<line num="157" type="method" name="generate_preview" visibility="public" complexity="8" crap="8" count="2"/>
57-
<line num="159" type="stmt" count="2"/>
58-
<line num="161" type="stmt" count="2"/>
59-
<line num="162" type="stmt" count="2"/>
60-
<line num="165" type="stmt" count="2"/>
61-
<line num="167" type="stmt" count="2"/>
62-
<line num="169" type="stmt" count="1"/>
63-
<line num="171" type="stmt" count="1"/>
64-
<line num="173" type="stmt" count="1"/>
65-
<line num="177" type="stmt" count="2"/>
66-
<line num="179" type="stmt" count="2"/>
67-
<line num="181" type="stmt" count="2"/>
68-
<line num="182" type="stmt" count="2"/>
69-
<line num="183" type="stmt" count="1"/>
70-
<line num="185" type="stmt" count="2"/>
71-
<line num="188" type="stmt" count="1"/>
72-
<line num="191" type="stmt" count="2"/>
73-
<line num="193" type="stmt" count="2"/>
74-
<metrics loc="196" ncloc="137" classes="1" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="57" coveredstatements="57" elements="65" coveredelements="65"/>
37+
<line num="95" type="stmt" count="1"/>
38+
<line num="97" type="stmt" count="1"/>
39+
<line num="100" type="stmt" count="1"/>
40+
<line num="111" type="method" name="append_markdown_option" visibility="public" complexity="1" crap="1" count="1"/>
41+
<line num="113" type="stmt" count="1"/>
42+
<line num="116" type="stmt" count="1"/>
43+
<line num="119" type="stmt" count="1"/>
44+
<line num="121" type="stmt" count="1"/>
45+
<line num="131" type="method" name="append_preview_button" visibility="public" complexity="1" crap="1" count="1"/>
46+
<line num="133" type="stmt" count="1"/>
47+
<line num="134" type="stmt" count="1"/>
48+
<line num="135" type="stmt" count="1"/>
49+
<line num="138" type="stmt" count="1"/>
50+
<line num="144" type="method" name="register_rest_route" visibility="public" complexity="1" crap="1" count="10"/>
51+
<line num="146" type="stmt" count="10"/>
52+
<line num="147" type="stmt" count="10"/>
53+
<line num="148" type="stmt" count="10"/>
54+
<line num="150" type="stmt" count="10"/>
55+
<line num="151" type="stmt" count="10"/>
56+
<line num="152" type="stmt" count="10"/>
57+
<line num="164" type="method" name="generate_preview" visibility="public" complexity="8" crap="8" count="2"/>
58+
<line num="166" type="stmt" count="2"/>
59+
<line num="168" type="stmt" count="2"/>
60+
<line num="169" type="stmt" count="2"/>
61+
<line num="172" type="stmt" count="2"/>
62+
<line num="174" type="stmt" count="2"/>
63+
<line num="176" type="stmt" count="1"/>
64+
<line num="178" type="stmt" count="1"/>
65+
<line num="180" type="stmt" count="1"/>
66+
<line num="184" type="stmt" count="2"/>
67+
<line num="186" type="stmt" count="2"/>
68+
<line num="188" type="stmt" count="2"/>
69+
<line num="189" type="stmt" count="2"/>
70+
<line num="190" type="stmt" count="1"/>
71+
<line num="192" type="stmt" count="2"/>
72+
<line num="195" type="stmt" count="1"/>
73+
<line num="198" type="stmt" count="2"/>
74+
<line num="200" type="stmt" count="2"/>
75+
<metrics loc="203" ncloc="139" classes="1" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="58" coveredstatements="58" elements="66" coveredelements="66"/>
7576
</file>
7677
</package>
77-
<metrics files="1" loc="196" ncloc="137" classes="1" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="57" coveredstatements="57" elements="65" coveredelements="65"/>
78+
<metrics files="1" loc="203" ncloc="139" classes="1" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="58" coveredstatements="58" elements="66" coveredelements="66"/>
7879
</project>
7980
</coverage>

inc/classes/class-comment-preview.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ protected function _setup_hooks() {
4343
*/
4444
public function enqueue_scripts() {
4545

46-
if ( is_singular( 'post' ) ) {
46+
/**
47+
* Filter to enable comment preview on custom post types.
48+
*
49+
* @param array List of post types.
50+
*/
51+
$post_types = apply_filters( 'wp_comment_preview_allowed_post_types', array( 'post' ) );
52+
53+
if ( is_singular( $post_types ) ) {
4754

4855
wp_register_script(
4956
'wp-comment-preview',

tests/class-test-comment-preview.php

+33
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,39 @@ public function test_enqueue_scripts() {
164164
$this->assertTrue( wp_script_is( 'wp-comment-preview' ) );
165165
}
166166

167+
/**
168+
* Covering enqueue_scripts() when script is loading for custom CPT.
169+
*/
170+
public function test_enqueue_scripts_for_cpt() {
171+
172+
register_post_type(
173+
'video',
174+
array(
175+
'public' => true,
176+
'show_in_rest' => true,
177+
)
178+
);
179+
180+
add_filter( 'wp_comment_preview_allowed_post_types', function( $post_types ) {
181+
182+
$post_types[] = 'video';
183+
184+
return $post_types;
185+
} );
186+
187+
$video_post = $this->factory->post->create(
188+
array(
189+
'post_type' => 'video',
190+
)
191+
);
192+
193+
$this->go_to( get_permalink( $video_post ) );
194+
195+
do_action( 'wp_enqueue_scripts' );
196+
197+
$this->assertTrue( wp_script_is( 'wp-comment-preview' ) );
198+
}
199+
167200
/**
168201
* Covering comment_form_fields().
169202
*/

0 commit comments

Comments
 (0)