Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Content | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
register_hooks | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
post_status | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
count_posts | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
comment_status | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\Listeners; |
4 | |
5 | use WP_Query; |
6 | |
7 | /** |
8 | * Monitors page/post events |
9 | */ |
10 | class Content extends Listener { |
11 | |
12 | /** |
13 | * Register the hooks for the subscriber |
14 | * |
15 | * @return void |
16 | */ |
17 | public function register_hooks() { |
18 | // Post status transitions |
19 | add_action( 'transition_post_status', array( $this, 'post_status' ), 10, 3 ); |
20 | |
21 | // transition comment status |
22 | add_action( 'transition_comment_status', array( $this, 'comment_status' ), 10, 3 ); |
23 | } |
24 | |
25 | /** |
26 | * Post status transition |
27 | * |
28 | * @param string $new_status The new post status |
29 | * @param string $old_status The old post status |
30 | * @param \WP_Post $post Post object |
31 | * |
32 | * @return void |
33 | */ |
34 | public function post_status( $new_status, $old_status, $post ) { |
35 | |
36 | $post_type = get_post_type_object( $post->post_type ); |
37 | |
38 | /** |
39 | * Ignore all post types that aren't public |
40 | */ |
41 | if ( ! $post_type || $post_type->public !== true ) { |
42 | return; |
43 | } |
44 | |
45 | $allowed_statuses = array( |
46 | 'draft', |
47 | 'pending', |
48 | 'publish', |
49 | 'new', |
50 | 'future', |
51 | 'private', |
52 | 'trash', |
53 | ); |
54 | if ( $new_status !== $old_status && in_array( $new_status, $allowed_statuses, true ) ) { |
55 | $data = array( |
56 | 'label_key' => 'new_status', |
57 | 'old_status' => $old_status, |
58 | 'new_status' => $new_status, |
59 | 'post' => $post, |
60 | ); |
61 | $this->push( 'content_status', $data ); |
62 | |
63 | if ( 'publish' === $new_status ) { |
64 | $count = $this->count_posts(); |
65 | |
66 | if ( 1 === $count ) { |
67 | $this->push( 'first_post_published', array( 'post' => $post ) ); |
68 | } |
69 | |
70 | if ( 5 === $count ) { |
71 | $this->push( 'fifth_post_published', array( 'post' => $post ) ); |
72 | } |
73 | } |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * Count published posts excluding the default 3: Sample Page, Hello World and the Privacy Page |
79 | * |
80 | * @return integer Number of published non-default posts |
81 | */ |
82 | public function count_posts() { |
83 | $types = get_post_types( array( 'public' => true ) ); |
84 | $args = array( |
85 | 'post_status' => 'publish', |
86 | 'post_type' => $types, |
87 | 'post__not_in' => array( 1, 2, 3 ), |
88 | ); |
89 | $query = new WP_Query( $args ); |
90 | |
91 | return $query->post_count; |
92 | } |
93 | |
94 | /** |
95 | * Comment status transition |
96 | * |
97 | * @param string $new_status The new comment status |
98 | * @param string $old_status The new comment status |
99 | * @param WP_Comment $comment Comment object |
100 | * |
101 | * @return void |
102 | */ |
103 | public function comment_status( $new_status, $old_status, $comment ) { |
104 | $allowed_statuses = array( |
105 | 'deleted', |
106 | 'approved', |
107 | 'unapproved', |
108 | 'spam', |
109 | ); |
110 | if ( $new_status !== $old_status && in_array( $new_status, $allowed_statuses, true ) ) { |
111 | $data = array( |
112 | 'label_key' => 'new_status', |
113 | 'old_status' => $old_status, |
114 | 'new_status' => $new_status, |
115 | 'comment' => $comment, |
116 | ); |
117 | $this->push( 'comment_status', $data ); |
118 | } |
119 | } |
120 | } |