|
1
|
|
|
<?php |
|
2
|
|
|
// Exit if access directly. |
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
4
|
|
|
exit; |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
class Give_Sequential_Donation_Number { |
|
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Instance. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 2.1.0 |
|
12
|
|
|
* @access private |
|
13
|
|
|
* @var |
|
14
|
|
|
*/ |
|
15
|
|
|
static private $instance; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Donation tile prefix |
|
19
|
|
|
* |
|
20
|
|
|
* @since 2.1.0 |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $donation_title_prefix = 'give-donation-'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Singleton pattern. |
|
27
|
|
|
* |
|
28
|
|
|
* @since 2.1.0 |
|
29
|
|
|
* @access private |
|
30
|
|
|
*/ |
|
31
|
|
|
private function __construct() { |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @since 2.1.0 |
|
39
|
|
|
* @access public |
|
40
|
|
|
* @return Give_Sequential_Donation_Number |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
public static function get_instance() { |
|
|
|
|
|
|
43
|
|
|
if ( null === static::$instance ) { |
|
44
|
|
|
self::$instance = new static(); |
|
45
|
|
|
|
|
46
|
|
|
self::$instance->init(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return self::$instance; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initialize the plugin, bailing if any required conditions are not met, |
|
54
|
|
|
* including minimum WooCommerce version |
|
55
|
|
|
* |
|
56
|
|
|
* @since 2.1.0 |
|
57
|
|
|
*/ |
|
58
|
|
|
public function init() { |
|
59
|
|
|
add_action( 'wp_insert_post', array( $this, '__save_donation_title' ), 10, 3 ); |
|
60
|
|
|
add_action( 'after_delete_post', array( $this, '__remove_serial_number' ), 10, 1 ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set serialize donation number as donation title. |
|
65
|
|
|
* Note: only for internal use |
|
66
|
|
|
* |
|
67
|
|
|
* @since 2.1.0 |
|
68
|
|
|
* @access public |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $donation_id |
|
71
|
|
|
* @param WP_Post $post |
|
72
|
|
|
* @param bool $existing_donation_updated |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __save_donation_title( $donation_id, $post, $existing_donation_updated ) { |
|
77
|
|
|
// Bailout |
|
78
|
|
|
if ( |
|
79
|
|
|
! give_is_setting_enabled( give_get_option( 'sequential-ordering_status', 'disabled' ) ) |
|
80
|
|
|
|| $existing_donation_updated |
|
81
|
|
|
|| 'give_payment' !== $post->post_type |
|
82
|
|
|
) { |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$serial_number = $this->__set_donation_number( $donation_id ); |
|
87
|
|
|
$serial_code = $this->set_number_padding( $serial_number ); |
|
88
|
|
|
|
|
89
|
|
|
// Add prefix. |
|
90
|
|
|
if ( $prefix = give_get_option( 'sequential-ordering_number_prefix', '' ) ) { |
|
91
|
|
|
$serial_code = $prefix . $serial_code; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Add suffix. |
|
95
|
|
|
if ( $suffix = give_get_option( 'sequential-ordering_number_suffix', '' ) ) { |
|
96
|
|
|
$serial_code = $serial_code . $suffix; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Filter the donation number |
|
101
|
|
|
* |
|
102
|
|
|
* @since 2.1.0 |
|
103
|
|
|
*/ |
|
104
|
|
|
$serial_code = apply_filters( |
|
105
|
|
|
'give_set_sequential_donation_title', |
|
106
|
|
|
give_time_do_tags( $serial_code ), |
|
107
|
|
|
$donation_id, |
|
108
|
|
|
$post, |
|
109
|
|
|
$existing_donation_updated, |
|
110
|
|
|
array( |
|
111
|
|
|
$serial_number, |
|
112
|
|
|
$prefix, |
|
113
|
|
|
$suffix |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
try { |
|
118
|
|
|
/* @var WP_Error $wp_error */ |
|
119
|
|
|
$wp_error = wp_update_post( |
|
120
|
|
|
array( |
|
121
|
|
|
'ID' => $donation_id, |
|
122
|
|
|
'post_name' => "{$this->donation_title_prefix}-{$serial_number}", |
|
123
|
|
|
'post_title' => trim( $serial_code ) |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
if ( is_wp_error( $wp_error ) ) { |
|
128
|
|
|
throw new Exception( $wp_error->get_error_message() ); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
give_update_option( 'sequential-ordering_number', ( $serial_number + 1 ) ); |
|
132
|
|
|
} catch ( Exception $e ) { |
|
133
|
|
|
error_log( "Give caught exception: {$e->getMessage()}" ); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set donation number |
|
139
|
|
|
* Note: only for internal use |
|
140
|
|
|
* |
|
141
|
|
|
* @since 2.1.0 |
|
142
|
|
|
* @access public |
|
143
|
|
|
* |
|
144
|
|
|
* @param int $donation_id |
|
145
|
|
|
* |
|
146
|
|
|
* @return int |
|
147
|
|
|
*/ |
|
148
|
|
|
public function __set_donation_number( $donation_id ) { |
|
149
|
|
|
$table_data = array( |
|
150
|
|
|
'payment_id' => $donation_id |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
// Customize sequential donation number starting point if needed. |
|
154
|
|
|
if ( |
|
155
|
|
|
get_option( '_give_reset_sequential_number' ) && |
|
156
|
|
|
( $number = give_get_option( 'sequential-ordering_number', 0 ) ) |
|
157
|
|
|
) { |
|
158
|
|
|
if ( Give()->sequential_donation_db->get_id_auto_increment_val() <= $number ) { |
|
159
|
|
|
delete_option( '_give_reset_sequential_number' ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$table_data['id'] = $number; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Filter the donation number |
|
168
|
|
|
* |
|
169
|
|
|
* @since 2.1 |
|
170
|
|
|
*/ |
|
171
|
|
|
return apply_filters( |
|
172
|
|
|
'give_set_sequential_donation_number', |
|
173
|
|
|
Give()->sequential_donation_db->insert( $table_data ), |
|
174
|
|
|
$table_data |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Remove sequential donation data |
|
181
|
|
|
* Note: only internal use. |
|
182
|
|
|
* |
|
183
|
|
|
* @since 2.1.0 |
|
184
|
|
|
* @access public |
|
185
|
|
|
* |
|
186
|
|
|
* @param $donation_id |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool |
|
189
|
|
|
*/ |
|
190
|
|
|
public function __remove_serial_number( $donation_id ) { |
|
191
|
|
|
return Give()->sequential_donation_db->delete( $this->get_serial_number( $donation_id ) ); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Set number padding in serial code. |
|
196
|
|
|
* |
|
197
|
|
|
* @since |
|
198
|
|
|
* @access public |
|
199
|
|
|
* |
|
200
|
|
|
* @param $serial_number |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function set_number_padding( $serial_number ) { |
|
205
|
|
|
if ( $number_padding = give_get_option( 'sequential-ordering_number_padding', 0 ) ) { |
|
206
|
|
|
$serial_number = str_pad( $serial_number, $number_padding, '0', STR_PAD_LEFT ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $serial_number; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Get donation number serial code |
|
214
|
|
|
* |
|
215
|
|
|
* @since 2.1.0 |
|
216
|
|
|
* @access public |
|
217
|
|
|
* |
|
218
|
|
|
* @param int|Give_Payment|WP_Post $donation |
|
219
|
|
|
* @param array $args |
|
220
|
|
|
* |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
public function get_serial_code( $donation, $args = array() ) { |
|
224
|
|
|
// Get id from object. |
|
225
|
|
|
if( ! is_numeric( $donation ) ) { |
|
226
|
|
|
if( $donation instanceof Give_Payment ) { |
|
227
|
|
|
$donation = $donation->ID; |
|
228
|
|
|
} elseif ( $donation instanceof WP_Post ){ |
|
|
|
|
|
|
229
|
|
|
$donation = $donation->ID; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
// Set default params. |
|
234
|
|
|
$args = wp_parse_args( |
|
235
|
|
|
$args, |
|
236
|
|
|
array( |
|
237
|
|
|
'with_hash' => false, |
|
238
|
|
|
'default' => true |
|
239
|
|
|
) |
|
240
|
|
|
); |
|
241
|
|
|
|
|
242
|
|
|
$serial_code = $args['default'] ? $donation : ''; |
|
243
|
|
|
|
|
244
|
|
|
if ( $donation_number = $this->get_serial_number( $donation ) ) { |
|
245
|
|
|
$serial_code = get_the_title( $donation ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
$serial_code = $args['with_hash'] ? "#{$serial_code}" : $serial_code; |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Filter the donation serial code |
|
252
|
|
|
* |
|
253
|
|
|
* @since 2.1.0 |
|
254
|
|
|
* |
|
255
|
|
|
* @param string $serial_code |
|
256
|
|
|
* @param string $donation Donation ID |
|
257
|
|
|
* @param array $args |
|
258
|
|
|
* @param string $donation_number |
|
259
|
|
|
*/ |
|
260
|
|
|
return apply_filters( 'give_get_donation_serial_code', $serial_code, $donation, $args, $donation_number ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Get serial number |
|
265
|
|
|
* |
|
266
|
|
|
* @since 2.1.0 |
|
267
|
|
|
* @access public |
|
268
|
|
|
* |
|
269
|
|
|
* @param int $donation_id_or_serial_code |
|
270
|
|
|
* |
|
271
|
|
|
* @return string |
|
272
|
|
|
*/ |
|
273
|
|
|
public function get_serial_number( $donation_id_or_serial_code ) { |
|
274
|
|
|
if ( is_numeric( $donation_id_or_serial_code ) ) { |
|
275
|
|
|
return Give()->sequential_donation_db->get_column_by( 'id', 'payment_id', $donation_id_or_serial_code ); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return $this->get_serial_number( $this->get_donation_id( $donation_id_or_serial_code ) ); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Get donation id with donation number or serial code |
|
284
|
|
|
* |
|
285
|
|
|
* @since 2.1.0 |
|
286
|
|
|
* @access public |
|
287
|
|
|
* |
|
288
|
|
|
* @param string $donation_number_or_serial_code |
|
289
|
|
|
* |
|
290
|
|
|
* @return string |
|
291
|
|
|
*/ |
|
292
|
|
|
public function get_donation_id( $donation_number_or_serial_code ) { |
|
293
|
|
|
global $wpdb; |
|
294
|
|
|
|
|
295
|
|
|
if ( is_numeric( $donation_number_or_serial_code ) ) { |
|
296
|
|
|
return Give()->sequential_donation_db->get_column_by( |
|
297
|
|
|
'payment_id', |
|
298
|
|
|
'id', |
|
299
|
|
|
$donation_number_or_serial_code |
|
300
|
|
|
); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
return $wpdb->get_var( |
|
304
|
|
|
$wpdb->prepare( |
|
305
|
|
|
" |
|
306
|
|
|
SELECT ID |
|
307
|
|
|
FROM $wpdb->posts |
|
308
|
|
|
WHERE post_title=%s |
|
309
|
|
|
", |
|
310
|
|
|
$donation_number_or_serial_code |
|
311
|
|
|
) |
|
312
|
|
|
); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Get maximum donation number |
|
317
|
|
|
* |
|
318
|
|
|
* @since 2.1.0 |
|
319
|
|
|
* @access public |
|
320
|
|
|
* |
|
321
|
|
|
* @return int |
|
322
|
|
|
*/ |
|
323
|
|
|
public function get_max_number() { |
|
324
|
|
|
global $wpdb; |
|
325
|
|
|
$table_name = Give()->sequential_donation_db->table_name; |
|
326
|
|
|
|
|
327
|
|
|
return absint( |
|
328
|
|
|
$wpdb->get_var( |
|
329
|
|
|
" |
|
330
|
|
|
SELECT ID |
|
331
|
|
|
FROM {$table_name} |
|
332
|
|
|
ORDER BY id DESC |
|
333
|
|
|
LIMIT 1 |
|
334
|
|
|
" |
|
335
|
|
|
) |
|
336
|
|
|
); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Get maximum donation id |
|
341
|
|
|
* |
|
342
|
|
|
* @since 2.1.0 |
|
343
|
|
|
* @access public |
|
344
|
|
|
* |
|
345
|
|
|
* @return int |
|
346
|
|
|
*/ |
|
347
|
|
|
public function get_max_donation_id() { |
|
348
|
|
|
global $wpdb; |
|
349
|
|
|
|
|
350
|
|
|
return absint( |
|
351
|
|
|
$wpdb->get_var( |
|
352
|
|
|
$wpdb->prepare( |
|
353
|
|
|
" |
|
354
|
|
|
SELECT ID |
|
355
|
|
|
FROM {$wpdb->posts} |
|
356
|
|
|
WHERE post_type=%s |
|
357
|
|
|
AND post_status=%s |
|
358
|
|
|
ORDER BY id DESC |
|
359
|
|
|
LIMIT 1 |
|
360
|
|
|
", |
|
361
|
|
|
'give_payment', |
|
362
|
|
|
'publish' |
|
363
|
|
|
) |
|
364
|
|
|
) |
|
365
|
|
|
); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Get maximum donation number |
|
370
|
|
|
* |
|
371
|
|
|
* @since 2.1.0 |
|
372
|
|
|
* @access public |
|
373
|
|
|
* |
|
374
|
|
|
* @return int |
|
375
|
|
|
*/ |
|
376
|
|
|
public function get_next_number() { |
|
377
|
|
|
$donation_id = $this->get_max_donation_id(); |
|
378
|
|
|
$next_number = $this->get_max_number(); |
|
379
|
|
|
|
|
380
|
|
|
if ( ! $this->get_serial_number( $donation_id ) ) { |
|
381
|
|
|
$next_number = $donation_id && ( $next_number < $donation_id ) ? |
|
382
|
|
|
$donation_id : |
|
383
|
|
|
$this->get_max_number(); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
return ( $next_number + 1 ); |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|