|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Notes class. |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Handles invoice notes. |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class WPInv_Notes { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class constructor. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() { |
|
19
|
|
|
|
|
20
|
|
|
// Filter inovice notes. |
|
21
|
|
|
add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
22
|
|
|
add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
23
|
|
|
|
|
24
|
|
|
// Fires after notes are loaded. |
|
25
|
|
|
do_action( 'wpinv_notes_init', $this ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Filters invoice notes query to only include our notes. |
|
30
|
|
|
* |
|
31
|
|
|
* @param WP_Comment_Query $query |
|
32
|
|
|
*/ |
|
33
|
|
|
public function set_invoice_note_type( $query ) { |
|
34
|
|
|
$post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
35
|
|
|
|
|
36
|
|
|
if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
37
|
|
|
$query->query_vars['type'] = 'wpinv_note'; |
|
38
|
|
|
} else { |
|
39
|
|
|
|
|
40
|
|
|
if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
41
|
|
|
$query->query_vars['type__not_in'] = array(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
45
|
|
|
$query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $query; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Exclude notes from the comments feed. |
|
53
|
|
|
*/ |
|
54
|
|
|
function wpinv_comment_feed_where( $where ){ |
|
|
|
|
|
|
55
|
|
|
return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns an array of invoice notes. |
|
60
|
|
|
* |
|
61
|
|
|
* @param int $invoice_id The invoice ID whose notes to retrieve. |
|
62
|
|
|
* @param string $type Optional. Pass in customer to only return customer notes. |
|
63
|
|
|
* @return WP_Comment[] |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
66
|
|
|
|
|
67
|
|
|
// Default comment args. |
|
68
|
|
|
$args = array( |
|
69
|
|
|
'post_id' => $invoice_id, |
|
70
|
|
|
'orderby' => 'comment_ID', |
|
71
|
|
|
'order' => 'ASC', |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
// Maybe only show customer comments. |
|
75
|
|
|
if ( $type == 'customer' ) { |
|
76
|
|
|
$args['meta_key'] = '_wpi_customer_note'; |
|
77
|
|
|
$args['meta_value'] = 1; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
81
|
|
|
|
|
82
|
|
|
return get_comments( $args ); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Saves an invoice comment. |
|
87
|
|
|
* |
|
88
|
|
|
* @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
89
|
|
|
* @param string $note The note content. |
|
90
|
|
|
* @param string $note_author The name of the author of the note. |
|
91
|
|
|
* @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
92
|
|
|
* @return int|false The new note's ID on success, false on failure. |
|
93
|
|
|
*/ |
|
94
|
|
|
function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Insert the comment. |
|
100
|
|
|
*/ |
|
101
|
|
|
$note_id = wp_insert_comment( |
|
102
|
|
|
wp_filter_comment( |
|
103
|
|
|
array( |
|
104
|
|
|
'comment_post_ID' => $invoice->get_id(), |
|
105
|
|
|
'comment_content' => $note, |
|
106
|
|
|
'comment_agent' => 'Invoicing', |
|
107
|
|
|
'user_id' => get_current_user_id(), |
|
108
|
|
|
'comment_author' => $note_author, |
|
109
|
|
|
'comment_author_IP' => wpinv_get_ip(), |
|
110
|
|
|
'comment_author_email' => $author_email, |
|
111
|
|
|
'comment_author_url' => $invoice->get_view_url(), |
|
112
|
|
|
'comment_type' => 'wpinv_note', |
|
113
|
|
|
) |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
118
|
|
|
|
|
119
|
|
|
// Are we notifying the customer? |
|
120
|
|
|
if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
121
|
|
|
return $note_id; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
125
|
|
|
do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
126
|
|
|
return $note_id; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
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.