|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; // Exit if accessed directly |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
class WPInv_Notes { |
|
7
|
|
|
private $invoice_post_type = 'wpi_invoice'; |
|
8
|
|
|
private $comment_type = 'wpinv_note'; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct() { |
|
11
|
|
|
$this->init(); |
|
12
|
|
|
$this->includes(); |
|
13
|
|
|
$this->actions(); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function init() { |
|
17
|
|
|
do_action( 'wpinv_class_notes_init', $this ); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function includes() { |
|
21
|
|
|
do_action( 'wpinv_class_notes_includes', $this ); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function actions() { |
|
25
|
|
|
// Secure inovice notes |
|
26
|
|
|
add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
27
|
|
|
add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
28
|
|
|
|
|
29
|
|
|
// Count comments |
|
30
|
|
|
add_filter( 'wp_count_comments', array( $this, 'wp_count_comments' ), 11, 2 ); |
|
31
|
|
|
|
|
32
|
|
|
// Delete comments count cache whenever there is a new comment or a comment status changes |
|
33
|
|
|
add_action( 'wp_insert_comment', array( $this, 'delete_comments_count_cache' ) ); |
|
34
|
|
|
add_action( 'wp_set_comment_status', array( $this, 'delete_comments_count_cache' ) ); |
|
35
|
|
|
|
|
36
|
|
|
do_action( 'wpinv_class_notes_actions', $this ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function set_invoice_note_type( $query ) { |
|
40
|
|
|
$post_ID = !empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
41
|
|
|
|
|
42
|
|
|
if ( $post_ID && in_array(get_post_type( $post_ID ), array($this->invoice_post_type, 'wpi_quote' )) ) { |
|
43
|
|
|
$query->query_vars['type__in'] = $this->comment_type; |
|
44
|
|
|
$query->query_vars['type__not_in'] = ''; |
|
45
|
|
|
} else { |
|
46
|
|
|
if ( isset( $query->query_vars['type__in'] ) && $type_in = $query->query_vars['type__in'] ) { |
|
47
|
|
|
if ( is_array( $type_in ) && in_array( $this->comment_type, $type_in ) ) { |
|
48
|
|
|
$key = array_search( $this->comment_type, $type_in ); |
|
49
|
|
|
unset( $query->query_vars['type__in'][$key] ); |
|
50
|
|
|
} else if ( !is_array( $type_in ) && $type_in == $this->comment_type ) { |
|
51
|
|
|
$query->query_vars['type__in'] = ''; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ( isset( $query->query_vars['type__not_in'] ) && $type_not_in = $query->query_vars['type__not_in'] ) { |
|
56
|
|
|
if ( is_array( $type_not_in ) && !in_array( $this->comment_type, $type_not_in ) ) { |
|
57
|
|
|
$query->query_vars['type__not_in'][] = $this->comment_type; |
|
58
|
|
|
} else if ( !is_array( $type_not_in ) && $type_not_in != $this->comment_type ) { |
|
59
|
|
|
$query->query_vars['type__not_in'] = (array)$query->query_vars['type__not_in']; |
|
60
|
|
|
$query->query_vars['type__not_in'][] = $this->comment_type; |
|
61
|
|
|
} |
|
62
|
|
|
} else { |
|
63
|
|
|
$query->query_vars['type__not_in'] = $this->comment_type; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $query; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function get_invoice_notes( $invoice_id = 0, $type = '' ) { |
|
71
|
|
|
$args = array( |
|
72
|
|
|
'post_id' => $invoice_id, |
|
73
|
|
|
'orderby' => 'comment_ID', |
|
74
|
|
|
'order' => 'ASC', |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
if ( $type == 'customer' ) { |
|
78
|
|
|
$args['meta_key'] = '_wpi_customer_note'; |
|
79
|
|
|
$args['meta_value'] = 1; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
83
|
|
|
|
|
84
|
|
|
return get_comments( $args ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Delete comments count cache whenever there is new comment or the |
|
89
|
|
|
* status of a comment changes. Cache will be regenerated next time |
|
90
|
|
|
* WPInv_Notes::wp_count_comments() is called. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function delete_comments_count_cache() { |
|
95
|
|
|
delete_transient( 'wpinv_count_comments' ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Remove invoice notes from wp_count_comments(). |
|
100
|
|
|
* |
|
101
|
|
|
* @since 1.0.0 |
|
102
|
|
|
* @param object $stats Comment stats. |
|
103
|
|
|
* @param int $post_id Post ID. |
|
104
|
|
|
* @return object |
|
105
|
|
|
*/ |
|
106
|
|
|
public function wp_count_comments( $stats, $post_id ) { |
|
107
|
|
|
global $wpdb; |
|
108
|
|
|
|
|
109
|
|
|
if ( 0 === $post_id ) { |
|
110
|
|
|
$stats = get_transient( 'wpinv_count_comments' ); |
|
111
|
|
|
|
|
112
|
|
|
if ( ! $stats ) { |
|
113
|
|
|
$stats = array(); |
|
114
|
|
|
|
|
115
|
|
|
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} WHERE comment_type NOT IN ('" . $this->comment_type . "') GROUP BY comment_approved", ARRAY_A ); |
|
116
|
|
|
|
|
117
|
|
|
$total = 0; |
|
118
|
|
|
$approved = array( |
|
119
|
|
|
'0' => 'moderated', |
|
120
|
|
|
'1' => 'approved', |
|
121
|
|
|
'spam' => 'spam', |
|
122
|
|
|
'trash' => 'trash', |
|
123
|
|
|
'post-trashed' => 'post-trashed', |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
foreach ( (array) $count as $row ) { |
|
127
|
|
|
// Do not count post-trashed toward totals. |
|
128
|
|
|
if ( 'post-trashed' !== $row['comment_approved'] && 'trash' !== $row['comment_approved'] ) { |
|
129
|
|
|
$total += $row['num_comments']; |
|
130
|
|
|
} |
|
131
|
|
|
if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
132
|
|
|
$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$stats['total_comments'] = $total; |
|
137
|
|
|
$stats['all'] = $total; |
|
138
|
|
|
foreach ( $approved as $key ) { |
|
139
|
|
|
if ( empty( $stats[ $key ] ) ) { |
|
140
|
|
|
$stats[ $key ] = 0; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$stats = (object) $stats; |
|
145
|
|
|
set_transient( 'wpinv_count_comments', $stats ); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $stats; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
function wpinv_comment_feed_where($where){ |
|
|
|
|
|
|
153
|
|
|
return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.