1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains error functions. |
4
|
|
|
* |
5
|
|
|
* @since 1.0.0 |
6
|
|
|
* @package Invoicing |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Returns the errors as html |
13
|
|
|
* |
14
|
|
|
* @param bool $clear whether or not to clear the errors. |
15
|
|
|
* @param bool $wrap whether or not to wrap the errors. |
16
|
|
|
* @since 1.0.19 |
17
|
|
|
*/ |
18
|
|
|
function getpaid_get_errors_html( $clear = true, $wrap = true ) { |
19
|
|
|
|
20
|
|
|
$errors = ''; |
21
|
|
|
foreach ( wpinv_get_errors() as $id => $error ) { |
22
|
|
|
$type = 'error'; |
23
|
|
|
|
24
|
|
|
if ( is_array( $error ) ) { |
25
|
|
|
$type = $error['type']; |
26
|
|
|
$error = $error['text']; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ( $wrap ) { |
30
|
|
|
|
31
|
|
|
$errors .= aui()->alert( |
32
|
|
|
array( |
33
|
|
|
'content' => wp_kses_post( $error ), |
34
|
|
|
'type' => $type, |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
} else { |
39
|
|
|
|
40
|
|
|
$id = esc_attr( $id ); |
41
|
|
|
$error = wp_kses_post( $error ); |
42
|
|
|
$errors .= "<div data-code='$id'>$error</div>"; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( $clear ) { |
47
|
|
|
wpinv_clear_errors(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $errors; |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Prints (then clears) all available errors. |
56
|
|
|
*/ |
57
|
|
|
function wpinv_print_errors() { |
58
|
|
|
echo wp_kses_post( getpaid_get_errors_html() ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns all available errors. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
function wpinv_get_errors() { |
67
|
|
|
|
68
|
|
|
// Contains known errors. |
69
|
|
|
$all_errors = array( |
70
|
|
|
'perm_cancel_subscription' => array( |
71
|
|
|
'type' => 'error', |
72
|
|
|
'text' => __( 'You do not have permission to cancel this subscription', 'invoicing' ), |
73
|
|
|
), |
74
|
|
|
'cannot_cancel_subscription' => array( |
75
|
|
|
'type' => 'error', |
76
|
|
|
'text' => __( 'This subscription cannot be cancelled as it is not active.', 'invoicing' ), |
77
|
|
|
), |
78
|
|
|
'cancelled_subscription' => array( |
79
|
|
|
'type' => 'success', |
80
|
|
|
'text' => __( 'Subscription cancelled successfully.', 'invoicing' ), |
81
|
|
|
), |
82
|
|
|
'address_updated' => array( |
83
|
|
|
'type' => 'success', |
84
|
|
|
'text' => __( 'Address updated successfully.', 'invoicing' ), |
85
|
|
|
), |
86
|
|
|
'perm_delete_invoice' => array( |
87
|
|
|
'type' => 'error', |
88
|
|
|
'text' => __( 'You do not have permission to delete this invoice', 'invoicing' ), |
89
|
|
|
), |
90
|
|
|
'cannot_delete_invoice' => array( |
91
|
|
|
'type' => 'error', |
92
|
|
|
'text' => __( 'This invoice cannot be deleted as it has already been paid.', 'invoicing' ), |
93
|
|
|
), |
94
|
|
|
'deleted_invoice' => array( |
95
|
|
|
'type' => 'success', |
96
|
|
|
'text' => __( 'Invoice deleted successfully.', 'invoicing' ), |
97
|
|
|
), |
98
|
|
|
'card_declined' => array( |
99
|
|
|
'type' => 'error', |
100
|
|
|
'text' => __( 'Your card was declined.', 'invoicing' ), |
101
|
|
|
), |
102
|
|
|
'invalid_currency' => array( |
103
|
|
|
'type' => 'error', |
104
|
|
|
'text' => __( 'The chosen payment gateway does not support this currency.', 'invoicing' ), |
105
|
|
|
), |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$errors = apply_filters( 'wpinv_errors', array() ); |
109
|
|
|
|
110
|
|
|
if ( isset( $_GET['wpinv-notice'] ) && isset( $all_errors[ $_GET['wpinv-notice'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
111
|
|
|
$errors[ $_GET['wpinv-notice'] ] = $all_errors[ $_GET['wpinv-notice'] ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( isset( $GLOBALS['wpinv_notice'] ) && isset( $all_errors[ $GLOBALS['wpinv_notice'] ] ) ) { |
115
|
|
|
$errors[ $GLOBALS['wpinv_notice'] ] = $all_errors[ $GLOBALS['wpinv_notice'] ]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ( isset( $GLOBALS['wpinv_custom_notice'] ) ) { |
119
|
|
|
$errors[ $GLOBALS['wpinv_custom_notice']['code'] ] = $GLOBALS['wpinv_custom_notice']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $errors; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Adds an error to the list of errors. |
127
|
|
|
* |
128
|
|
|
* @param string $error_id The error id. |
129
|
|
|
* @param string $error The error message. |
130
|
|
|
* @param string $type The error type. |
131
|
|
|
*/ |
132
|
|
|
function wpinv_set_error( $error_id, $message = '', $type = 'error' ) { |
133
|
|
|
|
134
|
|
|
if ( ! empty( $message ) ) { |
135
|
|
|
$GLOBALS['wpinv_custom_notice'] = array( |
136
|
|
|
'code' => $error_id, |
137
|
|
|
'type' => $type, |
138
|
|
|
'text' => $message, |
139
|
|
|
); |
140
|
|
|
} else { |
141
|
|
|
$GLOBALS['wpinv_notice'] = $error_id; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Checks if there is an error. |
147
|
|
|
* |
148
|
|
|
*/ |
149
|
|
|
function wpinv_has_errors() { |
150
|
|
|
return count( wpinv_get_errors() ) > 0; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Clears all error. |
155
|
|
|
* |
156
|
|
|
*/ |
157
|
|
|
function wpinv_clear_errors() { |
158
|
|
|
unset( $GLOBALS['wpinv_notice'] ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Clears a single error. |
163
|
|
|
* |
164
|
|
|
*/ |
165
|
|
|
function wpinv_unset_error() { |
166
|
|
|
unset( $GLOBALS['wpinv_notice'] ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Wrapper for _doing_it_wrong(). |
171
|
|
|
* |
172
|
|
|
* @since 1.0.19 |
173
|
|
|
* @param string $function Function used. |
174
|
|
|
* @param string $message Message to log. |
175
|
|
|
* @param string $version Version the message was added in. |
176
|
|
|
*/ |
177
|
|
|
function getpaid_doing_it_wrong( $function, $message, $version ) { |
178
|
|
|
|
179
|
|
|
$message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
180
|
|
|
|
181
|
|
|
if ( wp_doing_ajax() || defined( 'REST_REQUEST' ) ) { |
182
|
|
|
do_action( 'doing_it_wrong_run', $function, $message, $version ); |
183
|
|
|
error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
184
|
|
|
} else { |
185
|
|
|
_doing_it_wrong( esc_html( $function ), wp_kses_post( $message ), esc_html( $version ) ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Logs a debugging message. |
192
|
|
|
* |
193
|
|
|
* @param string $log The message to log. |
194
|
|
|
* @param string|bool $title The title of the message, or pass false to disable the backtrace. |
195
|
|
|
* @param string $file The file from which the error was logged. |
196
|
|
|
* @param string $line The line that contains the error. |
197
|
|
|
* @param bool $exit Whether or not to exit function execution. |
198
|
|
|
*/ |
199
|
|
|
function wpinv_error_log( $log, $title = '', $file = '', $line = '', $exit = false ) { |
200
|
|
|
|
201
|
|
|
if ( true === apply_filters( 'wpinv_log_errors', true ) ) { |
202
|
|
|
|
203
|
|
|
// Ensure the log is a scalar. |
204
|
|
|
if ( ! is_scalar( $log ) ) { |
|
|
|
|
205
|
|
|
$log = print_r( $log, true ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Add title. |
209
|
|
|
if ( ! empty( $title ) ) { |
210
|
|
|
$log = $title . ' ' . trim( $log ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// Add the file to the label. |
214
|
|
|
if ( ! empty( $file ) ) { |
215
|
|
|
$log .= ' in ' . $file; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// Add the line number to the label. |
219
|
|
|
if ( ! empty( $line ) ) { |
220
|
|
|
$log .= ' on line ' . $line; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// Log the message. |
224
|
|
|
error_log( trim( $log ) ); |
225
|
|
|
|
226
|
|
|
// ... and a backtrace. |
227
|
|
|
if ( false !== $title && false !== $file ) { |
228
|
|
|
error_log( 'Backtrace ' . wp_debug_backtrace_summary() ); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Maybe exit. |
233
|
|
|
if ( $exit ) { |
234
|
|
|
exit; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|