@@ -16,502 +16,502 @@ |
||
16 | 16 | */ |
17 | 17 | class GetPaid_Customers_Query { |
18 | 18 | |
19 | - /** |
|
20 | - * Query vars, after parsing |
|
21 | - * |
|
22 | - * @since 1.0.19 |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $query_vars = array(); |
|
26 | - |
|
27 | - /** |
|
28 | - * List of found customers. |
|
29 | - * |
|
30 | - * @since 1.0.19 |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $results; |
|
34 | - |
|
35 | - /** |
|
36 | - * Total number of found customers for the current query |
|
37 | - * |
|
38 | - * @since 1.0.19 |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - private $total_customers = 0; |
|
42 | - |
|
43 | - /** |
|
44 | - * The SQL query used to fetch matching customers. |
|
45 | - * |
|
46 | - * @since 1.0.19 |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - public $request; |
|
50 | - |
|
51 | - // SQL clauses |
|
52 | - |
|
53 | - /** |
|
54 | - * Contains the 'FIELDS' sql clause |
|
55 | - * |
|
56 | - * @since 1.0.19 |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - public $query_fields; |
|
60 | - |
|
61 | - /** |
|
62 | - * Contains the 'FROM' sql clause |
|
63 | - * |
|
64 | - * @since 1.0.19 |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - public $query_from; |
|
68 | - |
|
69 | - /** |
|
70 | - * Contains the 'WHERE' sql clause |
|
71 | - * |
|
72 | - * @since 1.0.19 |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - public $query_where; |
|
76 | - |
|
77 | - /** |
|
78 | - * Contains the 'ORDER BY' sql clause |
|
79 | - * |
|
80 | - * @since 1.0.19 |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - public $query_orderby; |
|
84 | - |
|
85 | - /** |
|
86 | - * Contains the 'LIMIT' sql clause |
|
87 | - * |
|
88 | - * @since 1.0.19 |
|
89 | - * @var string |
|
90 | - */ |
|
91 | - public $query_limit; |
|
92 | - |
|
93 | - /** |
|
94 | - * Class constructor. |
|
95 | - * |
|
96 | - * @since 1.0.19 |
|
97 | - * |
|
98 | - * @param null|string|array $query Optional. The query variables. |
|
99 | - */ |
|
100 | - public function __construct( $query = null ) { |
|
101 | - if ( ! is_null( $query ) ) { |
|
102 | - $this->prepare_query( $query ); |
|
103 | - $this->query(); |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Fills in missing query variables with default values. |
|
109 | - * |
|
110 | - * @since 1.0.19 |
|
111 | - * |
|
112 | - * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
113 | - * @return array Complete query variables with undefined ones filled in with defaults. |
|
114 | - */ |
|
115 | - public static function fill_query_vars( $args ) { |
|
116 | - $defaults = array( |
|
117 | - 'include' => array(), |
|
118 | - 'exclude' => array(), |
|
119 | - 'orderby' => 'id', |
|
120 | - 'order' => 'DESC', |
|
121 | - 'offset' => '', |
|
122 | - 'number' => 10, |
|
123 | - 'paged' => 1, |
|
124 | - 'count_total' => true, |
|
125 | - 'fields' => 'all', |
|
126 | - 's' => '', |
|
127 | - ); |
|
128 | - |
|
129 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
130 | - $defaults[ $field ] = 'any'; |
|
131 | - |
|
132 | - if ( '%f' === $type || '%d' === $type ) { |
|
133 | - $defaults[ $field . '_min' ] = ''; |
|
134 | - $defaults[ $field . '_max' ] = ''; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return wp_parse_args( $args, $defaults ); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Prepare the query variables. |
|
143 | - * |
|
144 | - * @since 1.0.19 |
|
145 | - * |
|
146 | - * @see self::fill_query_vars() For allowede args and their defaults. |
|
147 | - */ |
|
148 | - public function prepare_query( $query = array() ) { |
|
149 | - global $wpdb; |
|
150 | - |
|
151 | - if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
152 | - $this->query_limit = null; |
|
153 | - $this->query_vars = $this->fill_query_vars( $query ); |
|
154 | - } |
|
155 | - |
|
156 | - if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
157 | - $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
158 | - } |
|
159 | - |
|
160 | - do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
161 | - |
|
162 | - // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
|
163 | - $qv = & $this->query_vars; |
|
164 | - $qv = $this->fill_query_vars( $qv ); |
|
165 | - $table = $wpdb->prefix . 'getpaid_customers'; |
|
166 | - $this->query_from = "FROM $table"; |
|
167 | - |
|
168 | - // Prepare query fields. |
|
169 | - $this->prepare_query_fields( $qv, $table ); |
|
170 | - |
|
171 | - // Prepare query where. |
|
172 | - $this->prepare_query_where( $qv, $table ); |
|
173 | - |
|
174 | - // Prepare query order. |
|
175 | - $this->prepare_query_order( $qv, $table ); |
|
176 | - |
|
177 | - // limit |
|
178 | - if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
179 | - if ( $qv['offset'] ) { |
|
180 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
181 | - } else { |
|
182 | - $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Prepares the query fields. |
|
191 | - * |
|
192 | - * @since 1.0.19 |
|
193 | - * |
|
194 | - * @param array $qv Query vars. |
|
195 | - * @param string $table Table name. |
|
196 | - */ |
|
197 | - protected function prepare_query_fields( &$qv, $table ) { |
|
198 | - |
|
199 | - if ( is_array( $qv['fields'] ) ) { |
|
200 | - $qv['fields'] = array_unique( $qv['fields'] ); |
|
201 | - $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
202 | - |
|
203 | - $query_fields = array(); |
|
204 | - foreach ( $qv['fields'] as $field ) { |
|
205 | - if ( ! in_array( $field, $allowed_fields ) ) { |
|
206 | - continue; |
|
207 | - } |
|
208 | - |
|
209 | - $field = sanitize_key( $field ); |
|
210 | - $query_fields[] = "$table.`$field`"; |
|
211 | - } |
|
212 | - $this->query_fields = implode( ',', $query_fields ); |
|
213 | - } else { |
|
214 | - $this->query_fields = "$table.*"; |
|
215 | - } |
|
216 | - |
|
217 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
218 | - $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
219 | - } |
|
220 | - |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Prepares the query where. |
|
225 | - * |
|
226 | - * @since 1.0.19 |
|
227 | - * |
|
228 | - * @param array $qv Query vars. |
|
229 | - * @param string $table Table name. |
|
230 | - */ |
|
231 | - protected function prepare_query_where( &$qv, $table ) { |
|
232 | - global $wpdb; |
|
233 | - $this->query_where = 'WHERE 1=1'; |
|
234 | - |
|
235 | - // Fields. |
|
236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | - if ( 'any' !== $qv[ $field ] ) { |
|
238 | - |
|
239 | - // In. |
|
240 | - if ( is_array( $qv[ $field ] ) ) { |
|
241 | - $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
242 | - $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
243 | - } elseif ( ! empty( $qv[ $field ] ) ) { |
|
244 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - // Min/Max. |
|
249 | - if ( '%f' === $type || '%d' === $type ) { |
|
250 | - |
|
251 | - // Min. |
|
252 | - if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
253 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
254 | - } |
|
255 | - |
|
256 | - // Max. |
|
257 | - if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
258 | - $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
259 | - } |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - if ( ! empty( $qv['include'] ) ) { |
|
264 | - $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
265 | - $this->query_where .= " AND $table.`id` IN ($include)"; |
|
266 | - } elseif ( ! empty( $qv['exclude'] ) ) { |
|
267 | - $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
268 | - $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
269 | - } |
|
270 | - |
|
271 | - // Date queries are allowed for the customer creation date. |
|
272 | - if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
273 | - $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
274 | - $this->query_where .= $date_created_query->get_sql(); |
|
275 | - } |
|
276 | - |
|
277 | - // Search. |
|
278 | - if ( ! empty( $qv['s'] ) ) { |
|
279 | - $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Used internally to generate an SQL string for searching across multiple columns |
|
285 | - * |
|
286 | - * @since 1.2.7 |
|
287 | - * |
|
288 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
289 | - * |
|
290 | - * @param string $string The string to search for. |
|
291 | - * @return string |
|
292 | - */ |
|
293 | - protected function get_search_sql( $string ) { |
|
294 | - global $wpdb; |
|
295 | - |
|
296 | - $searches = array(); |
|
297 | - $string = trim( $string, '%' ); |
|
298 | - $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
299 | - |
|
300 | - foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
301 | - if ( 'id' === $col || 'user_id' === $col ) { |
|
302 | - $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
303 | - } else { |
|
304 | - $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
305 | - } |
|
306 | - } |
|
307 | - |
|
308 | - return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Prepares the query order. |
|
313 | - * |
|
314 | - * @since 1.0.19 |
|
315 | - * |
|
316 | - * @param array $qv Query vars. |
|
317 | - * @param string $table Table name. |
|
318 | - */ |
|
319 | - protected function prepare_query_order( &$qv, $table ) { |
|
320 | - |
|
321 | - // sorting. |
|
322 | - $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
323 | - $order = $this->parse_order( $qv['order'] ); |
|
324 | - |
|
325 | - // Default order is by 'id' (latest customers). |
|
326 | - if ( empty( $qv['orderby'] ) ) { |
|
327 | - $qv['orderby'] = array( 'id' ); |
|
328 | - } |
|
329 | - |
|
330 | - // 'orderby' values may be an array, comma- or space-separated list. |
|
331 | - $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
332 | - |
|
333 | - $orderby_array = array(); |
|
334 | - foreach ( $ordersby as $_key => $_value ) { |
|
335 | - |
|
336 | - if ( is_int( $_key ) ) { |
|
337 | - // Integer key means this is a flat array of 'orderby' fields. |
|
338 | - $_orderby = $_value; |
|
339 | - $_order = $order; |
|
340 | - } else { |
|
341 | - // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
342 | - $_orderby = $_key; |
|
343 | - $_order = $_value; |
|
344 | - } |
|
345 | - |
|
346 | - $parsed = $this->parse_orderby( $_orderby, $table ); |
|
347 | - |
|
348 | - if ( $parsed ) { |
|
349 | - $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
350 | - } |
|
351 | - } |
|
352 | - |
|
353 | - // If no valid clauses were found, order by id. |
|
354 | - if ( empty( $orderby_array ) ) { |
|
355 | - $orderby_array[] = "id $order"; |
|
356 | - } |
|
357 | - |
|
358 | - $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
359 | - |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * Execute the query, with the current variables. |
|
364 | - * |
|
365 | - * @since 1.0.19 |
|
366 | - * |
|
367 | - * @global wpdb $wpdb WordPress database abstraction object. |
|
368 | - */ |
|
369 | - public function query() { |
|
370 | - global $wpdb; |
|
371 | - |
|
372 | - $qv =& $this->query_vars; |
|
373 | - |
|
374 | - // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
|
375 | - // total_customers property. |
|
376 | - $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
377 | - |
|
378 | - if ( null === $this->results ) { |
|
379 | - $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
380 | - |
|
381 | - if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
382 | - $this->results = $wpdb->get_results( $this->request ); |
|
383 | - } else { |
|
384 | - $this->results = $wpdb->get_col( $this->request ); |
|
385 | - } |
|
386 | - |
|
387 | - if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
388 | - $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
389 | - $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
390 | - } |
|
391 | - } |
|
392 | - |
|
393 | - if ( 'all' === $qv['fields'] ) { |
|
394 | - foreach ( $this->results as $key => $customer ) { |
|
395 | - $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
396 | - $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
397 | - $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
398 | - $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Set cache |
|
406 | - * |
|
407 | - * @param string $id |
|
408 | - * @param mixed $data |
|
409 | - * @param string $group |
|
410 | - * @param integer $expire |
|
411 | - * @return boolean |
|
412 | - */ |
|
413 | - public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
414 | - |
|
415 | - if ( empty( $key ) ) { |
|
416 | - return false; |
|
417 | - } |
|
418 | - |
|
419 | - wp_cache_set( $key, $data, $group, $expire ); |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Retrieve query variable. |
|
424 | - * |
|
425 | - * @since 1.0.19 |
|
426 | - * |
|
427 | - * @param string $query_var Query variable key. |
|
428 | - * @return mixed |
|
429 | - */ |
|
430 | - public function get( $query_var ) { |
|
431 | - if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
432 | - return $this->query_vars[ $query_var ]; |
|
433 | - } |
|
434 | - |
|
435 | - return null; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Set query variable. |
|
440 | - * |
|
441 | - * @since 1.0.19 |
|
442 | - * |
|
443 | - * @param string $query_var Query variable key. |
|
444 | - * @param mixed $value Query variable value. |
|
445 | - */ |
|
446 | - public function set( $query_var, $value ) { |
|
447 | - $this->query_vars[ $query_var ] = $value; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Return the list of customers. |
|
452 | - * |
|
453 | - * @since 1.0.19 |
|
454 | - * |
|
455 | - * @return GetPaid_Customer[]|array Found customers. |
|
456 | - */ |
|
457 | - public function get_results() { |
|
458 | - return $this->results; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Return the total number of customers for the current query. |
|
463 | - * |
|
464 | - * @since 1.0.19 |
|
465 | - * |
|
466 | - * @return int Number of total customers. |
|
467 | - */ |
|
468 | - public function get_total() { |
|
469 | - return $this->total_customers; |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Parse and sanitize 'orderby' keys passed to the customers query. |
|
474 | - * |
|
475 | - * @since 1.0.19 |
|
476 | - * |
|
477 | - * @param string $orderby Alias for the field to order by. |
|
478 | - * @param string $table The current table. |
|
479 | - * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
480 | - */ |
|
481 | - protected function parse_orderby( $orderby, $table ) { |
|
482 | - |
|
483 | - $_orderby = ''; |
|
484 | - if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
485 | - $_orderby = "$table.`$orderby`"; |
|
486 | - } elseif ( 'id' === strtolower( $orderby ) ) { |
|
487 | - $_orderby = "$table.id"; |
|
488 | - } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
489 | - $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
490 | - $include_sql = implode( ',', $include ); |
|
491 | - $_orderby = "FIELD( $table.id, $include_sql )"; |
|
492 | - } |
|
493 | - |
|
494 | - return $_orderby; |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
499 | - * |
|
500 | - * @since 1.0.19 |
|
501 | - * |
|
502 | - * @param string $order The 'order' query variable. |
|
503 | - * @return string The sanitized 'order' query variable. |
|
504 | - */ |
|
505 | - protected function parse_order( $order ) { |
|
506 | - if ( ! is_string( $order ) || empty( $order ) ) { |
|
507 | - return 'DESC'; |
|
508 | - } |
|
509 | - |
|
510 | - if ( 'ASC' === strtoupper( $order ) ) { |
|
511 | - return 'ASC'; |
|
512 | - } else { |
|
513 | - return 'DESC'; |
|
514 | - } |
|
515 | - } |
|
19 | + /** |
|
20 | + * Query vars, after parsing |
|
21 | + * |
|
22 | + * @since 1.0.19 |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $query_vars = array(); |
|
26 | + |
|
27 | + /** |
|
28 | + * List of found customers. |
|
29 | + * |
|
30 | + * @since 1.0.19 |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $results; |
|
34 | + |
|
35 | + /** |
|
36 | + * Total number of found customers for the current query |
|
37 | + * |
|
38 | + * @since 1.0.19 |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + private $total_customers = 0; |
|
42 | + |
|
43 | + /** |
|
44 | + * The SQL query used to fetch matching customers. |
|
45 | + * |
|
46 | + * @since 1.0.19 |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + public $request; |
|
50 | + |
|
51 | + // SQL clauses |
|
52 | + |
|
53 | + /** |
|
54 | + * Contains the 'FIELDS' sql clause |
|
55 | + * |
|
56 | + * @since 1.0.19 |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + public $query_fields; |
|
60 | + |
|
61 | + /** |
|
62 | + * Contains the 'FROM' sql clause |
|
63 | + * |
|
64 | + * @since 1.0.19 |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + public $query_from; |
|
68 | + |
|
69 | + /** |
|
70 | + * Contains the 'WHERE' sql clause |
|
71 | + * |
|
72 | + * @since 1.0.19 |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + public $query_where; |
|
76 | + |
|
77 | + /** |
|
78 | + * Contains the 'ORDER BY' sql clause |
|
79 | + * |
|
80 | + * @since 1.0.19 |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + public $query_orderby; |
|
84 | + |
|
85 | + /** |
|
86 | + * Contains the 'LIMIT' sql clause |
|
87 | + * |
|
88 | + * @since 1.0.19 |
|
89 | + * @var string |
|
90 | + */ |
|
91 | + public $query_limit; |
|
92 | + |
|
93 | + /** |
|
94 | + * Class constructor. |
|
95 | + * |
|
96 | + * @since 1.0.19 |
|
97 | + * |
|
98 | + * @param null|string|array $query Optional. The query variables. |
|
99 | + */ |
|
100 | + public function __construct( $query = null ) { |
|
101 | + if ( ! is_null( $query ) ) { |
|
102 | + $this->prepare_query( $query ); |
|
103 | + $this->query(); |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Fills in missing query variables with default values. |
|
109 | + * |
|
110 | + * @since 1.0.19 |
|
111 | + * |
|
112 | + * @param string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`. |
|
113 | + * @return array Complete query variables with undefined ones filled in with defaults. |
|
114 | + */ |
|
115 | + public static function fill_query_vars( $args ) { |
|
116 | + $defaults = array( |
|
117 | + 'include' => array(), |
|
118 | + 'exclude' => array(), |
|
119 | + 'orderby' => 'id', |
|
120 | + 'order' => 'DESC', |
|
121 | + 'offset' => '', |
|
122 | + 'number' => 10, |
|
123 | + 'paged' => 1, |
|
124 | + 'count_total' => true, |
|
125 | + 'fields' => 'all', |
|
126 | + 's' => '', |
|
127 | + ); |
|
128 | + |
|
129 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
130 | + $defaults[ $field ] = 'any'; |
|
131 | + |
|
132 | + if ( '%f' === $type || '%d' === $type ) { |
|
133 | + $defaults[ $field . '_min' ] = ''; |
|
134 | + $defaults[ $field . '_max' ] = ''; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return wp_parse_args( $args, $defaults ); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Prepare the query variables. |
|
143 | + * |
|
144 | + * @since 1.0.19 |
|
145 | + * |
|
146 | + * @see self::fill_query_vars() For allowede args and their defaults. |
|
147 | + */ |
|
148 | + public function prepare_query( $query = array() ) { |
|
149 | + global $wpdb; |
|
150 | + |
|
151 | + if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
|
152 | + $this->query_limit = null; |
|
153 | + $this->query_vars = $this->fill_query_vars( $query ); |
|
154 | + } |
|
155 | + |
|
156 | + if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) { |
|
157 | + $this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] ); |
|
158 | + } |
|
159 | + |
|
160 | + do_action( 'getpaid_pre_get_customers', array( &$this ) ); |
|
161 | + |
|
162 | + // Ensure that query vars are filled after 'getpaid_pre_get_customers'. |
|
163 | + $qv = & $this->query_vars; |
|
164 | + $qv = $this->fill_query_vars( $qv ); |
|
165 | + $table = $wpdb->prefix . 'getpaid_customers'; |
|
166 | + $this->query_from = "FROM $table"; |
|
167 | + |
|
168 | + // Prepare query fields. |
|
169 | + $this->prepare_query_fields( $qv, $table ); |
|
170 | + |
|
171 | + // Prepare query where. |
|
172 | + $this->prepare_query_where( $qv, $table ); |
|
173 | + |
|
174 | + // Prepare query order. |
|
175 | + $this->prepare_query_order( $qv, $table ); |
|
176 | + |
|
177 | + // limit |
|
178 | + if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
|
179 | + if ( $qv['offset'] ) { |
|
180 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] ); |
|
181 | + } else { |
|
182 | + $this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + do_action_ref_array( 'getpaid_after_customers_query', array( &$this ) ); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Prepares the query fields. |
|
191 | + * |
|
192 | + * @since 1.0.19 |
|
193 | + * |
|
194 | + * @param array $qv Query vars. |
|
195 | + * @param string $table Table name. |
|
196 | + */ |
|
197 | + protected function prepare_query_fields( &$qv, $table ) { |
|
198 | + |
|
199 | + if ( is_array( $qv['fields'] ) ) { |
|
200 | + $qv['fields'] = array_unique( $qv['fields'] ); |
|
201 | + $allowed_fields = array_keys( GetPaid_Customer_Data_Store::get_database_fields() ); |
|
202 | + |
|
203 | + $query_fields = array(); |
|
204 | + foreach ( $qv['fields'] as $field ) { |
|
205 | + if ( ! in_array( $field, $allowed_fields ) ) { |
|
206 | + continue; |
|
207 | + } |
|
208 | + |
|
209 | + $field = sanitize_key( $field ); |
|
210 | + $query_fields[] = "$table.`$field`"; |
|
211 | + } |
|
212 | + $this->query_fields = implode( ',', $query_fields ); |
|
213 | + } else { |
|
214 | + $this->query_fields = "$table.*"; |
|
215 | + } |
|
216 | + |
|
217 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
218 | + $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
|
219 | + } |
|
220 | + |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Prepares the query where. |
|
225 | + * |
|
226 | + * @since 1.0.19 |
|
227 | + * |
|
228 | + * @param array $qv Query vars. |
|
229 | + * @param string $table Table name. |
|
230 | + */ |
|
231 | + protected function prepare_query_where( &$qv, $table ) { |
|
232 | + global $wpdb; |
|
233 | + $this->query_where = 'WHERE 1=1'; |
|
234 | + |
|
235 | + // Fields. |
|
236 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | + if ( 'any' !== $qv[ $field ] ) { |
|
238 | + |
|
239 | + // In. |
|
240 | + if ( is_array( $qv[ $field ] ) ) { |
|
241 | + $in = join( ',', array_fill( 0, count( $qv[ $field ] ), $type ) ); |
|
242 | + $this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $in )", $qv[ $field ] ); |
|
243 | + } elseif ( ! empty( $qv[ $field ] ) ) { |
|
244 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` = $type", $qv[ $field ] ); |
|
245 | + } |
|
246 | + } |
|
247 | + |
|
248 | + // Min/Max. |
|
249 | + if ( '%f' === $type || '%d' === $type ) { |
|
250 | + |
|
251 | + // Min. |
|
252 | + if ( is_numeric( $qv[ $field . '_min' ] ) ) { |
|
253 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` >= $type", $qv[ $field . '_min' ] ); |
|
254 | + } |
|
255 | + |
|
256 | + // Max. |
|
257 | + if ( is_numeric( $qv[ $field . '_max' ] ) ) { |
|
258 | + $this->query_where .= $wpdb->prepare( " AND $table.`$field` <= $type", $qv[ $field . '_max' ] ); |
|
259 | + } |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + if ( ! empty( $qv['include'] ) ) { |
|
264 | + $include = implode( ',', wp_parse_id_list( $qv['include'] ) ); |
|
265 | + $this->query_where .= " AND $table.`id` IN ($include)"; |
|
266 | + } elseif ( ! empty( $qv['exclude'] ) ) { |
|
267 | + $exclude = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
|
268 | + $this->query_where .= " AND $table.`id` NOT IN ($exclude)"; |
|
269 | + } |
|
270 | + |
|
271 | + // Date queries are allowed for the customer creation date. |
|
272 | + if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) { |
|
273 | + $date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.date_created" ); |
|
274 | + $this->query_where .= $date_created_query->get_sql(); |
|
275 | + } |
|
276 | + |
|
277 | + // Search. |
|
278 | + if ( ! empty( $qv['s'] ) ) { |
|
279 | + $this->query_where .= $this->get_search_sql( $qv['s'] ); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Used internally to generate an SQL string for searching across multiple columns |
|
285 | + * |
|
286 | + * @since 1.2.7 |
|
287 | + * |
|
288 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
289 | + * |
|
290 | + * @param string $string The string to search for. |
|
291 | + * @return string |
|
292 | + */ |
|
293 | + protected function get_search_sql( $string ) { |
|
294 | + global $wpdb; |
|
295 | + |
|
296 | + $searches = array(); |
|
297 | + $string = trim( $string, '%' ); |
|
298 | + $like = '%' . $wpdb->esc_like( $string ) . '%'; |
|
299 | + |
|
300 | + foreach ( array_keys( GetPaid_Customer_Data_Store::get_database_fields() ) as $col ) { |
|
301 | + if ( 'id' === $col || 'user_id' === $col ) { |
|
302 | + $searches[] = $wpdb->prepare( "$col = %s", $string ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
303 | + } else { |
|
304 | + $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
|
305 | + } |
|
306 | + } |
|
307 | + |
|
308 | + return ' AND (' . implode( ' OR ', $searches ) . ')'; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Prepares the query order. |
|
313 | + * |
|
314 | + * @since 1.0.19 |
|
315 | + * |
|
316 | + * @param array $qv Query vars. |
|
317 | + * @param string $table Table name. |
|
318 | + */ |
|
319 | + protected function prepare_query_order( &$qv, $table ) { |
|
320 | + |
|
321 | + // sorting. |
|
322 | + $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
|
323 | + $order = $this->parse_order( $qv['order'] ); |
|
324 | + |
|
325 | + // Default order is by 'id' (latest customers). |
|
326 | + if ( empty( $qv['orderby'] ) ) { |
|
327 | + $qv['orderby'] = array( 'id' ); |
|
328 | + } |
|
329 | + |
|
330 | + // 'orderby' values may be an array, comma- or space-separated list. |
|
331 | + $ordersby = array_filter( wpinv_parse_list( $qv['orderby'] ) ); |
|
332 | + |
|
333 | + $orderby_array = array(); |
|
334 | + foreach ( $ordersby as $_key => $_value ) { |
|
335 | + |
|
336 | + if ( is_int( $_key ) ) { |
|
337 | + // Integer key means this is a flat array of 'orderby' fields. |
|
338 | + $_orderby = $_value; |
|
339 | + $_order = $order; |
|
340 | + } else { |
|
341 | + // Non-integer key means that the key is the field and the value is ASC/DESC. |
|
342 | + $_orderby = $_key; |
|
343 | + $_order = $_value; |
|
344 | + } |
|
345 | + |
|
346 | + $parsed = $this->parse_orderby( $_orderby, $table ); |
|
347 | + |
|
348 | + if ( $parsed ) { |
|
349 | + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
|
350 | + } |
|
351 | + } |
|
352 | + |
|
353 | + // If no valid clauses were found, order by id. |
|
354 | + if ( empty( $orderby_array ) ) { |
|
355 | + $orderby_array[] = "id $order"; |
|
356 | + } |
|
357 | + |
|
358 | + $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
|
359 | + |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * Execute the query, with the current variables. |
|
364 | + * |
|
365 | + * @since 1.0.19 |
|
366 | + * |
|
367 | + * @global wpdb $wpdb WordPress database abstraction object. |
|
368 | + */ |
|
369 | + public function query() { |
|
370 | + global $wpdb; |
|
371 | + |
|
372 | + $qv =& $this->query_vars; |
|
373 | + |
|
374 | + // Return a non-null value to bypass the default GetPaid customers query and remember to set the |
|
375 | + // total_customers property. |
|
376 | + $this->results = apply_filters_ref_array( 'getpaid_customers_pre_query', array( null, &$this ) ); |
|
377 | + |
|
378 | + if ( null === $this->results ) { |
|
379 | + $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; |
|
380 | + |
|
381 | + if ( ( is_array( $qv['fields'] ) && 1 !== count( $qv['fields'] ) ) || 'all' === $qv['fields'] ) { |
|
382 | + $this->results = $wpdb->get_results( $this->request ); |
|
383 | + } else { |
|
384 | + $this->results = $wpdb->get_col( $this->request ); |
|
385 | + } |
|
386 | + |
|
387 | + if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { |
|
388 | + $found_customers_query = apply_filters( 'getpaid_found_customers_query', 'SELECT FOUND_ROWS()', $this ); |
|
389 | + $this->total_customers = (int) $wpdb->get_var( $found_customers_query ); |
|
390 | + } |
|
391 | + } |
|
392 | + |
|
393 | + if ( 'all' === $qv['fields'] ) { |
|
394 | + foreach ( $this->results as $key => $customer ) { |
|
395 | + $this->set_cache( $customer->id, $customer, 'getpaid_customers' ); |
|
396 | + $this->set_cache( $customer->user_id, $customer->id, 'getpaid_customer_ids_by_user_id' ); |
|
397 | + $this->set_cache( $customer->email, $customer->id, 'getpaid_customer_ids_by_email' ); |
|
398 | + $this->results[ $key ] = new GetPaid_Customer( $customer ); |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Set cache |
|
406 | + * |
|
407 | + * @param string $id |
|
408 | + * @param mixed $data |
|
409 | + * @param string $group |
|
410 | + * @param integer $expire |
|
411 | + * @return boolean |
|
412 | + */ |
|
413 | + public function set_cache( $key, $data, $group = '', $expire = 0 ) { |
|
414 | + |
|
415 | + if ( empty( $key ) ) { |
|
416 | + return false; |
|
417 | + } |
|
418 | + |
|
419 | + wp_cache_set( $key, $data, $group, $expire ); |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Retrieve query variable. |
|
424 | + * |
|
425 | + * @since 1.0.19 |
|
426 | + * |
|
427 | + * @param string $query_var Query variable key. |
|
428 | + * @return mixed |
|
429 | + */ |
|
430 | + public function get( $query_var ) { |
|
431 | + if ( isset( $this->query_vars[ $query_var ] ) ) { |
|
432 | + return $this->query_vars[ $query_var ]; |
|
433 | + } |
|
434 | + |
|
435 | + return null; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Set query variable. |
|
440 | + * |
|
441 | + * @since 1.0.19 |
|
442 | + * |
|
443 | + * @param string $query_var Query variable key. |
|
444 | + * @param mixed $value Query variable value. |
|
445 | + */ |
|
446 | + public function set( $query_var, $value ) { |
|
447 | + $this->query_vars[ $query_var ] = $value; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Return the list of customers. |
|
452 | + * |
|
453 | + * @since 1.0.19 |
|
454 | + * |
|
455 | + * @return GetPaid_Customer[]|array Found customers. |
|
456 | + */ |
|
457 | + public function get_results() { |
|
458 | + return $this->results; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Return the total number of customers for the current query. |
|
463 | + * |
|
464 | + * @since 1.0.19 |
|
465 | + * |
|
466 | + * @return int Number of total customers. |
|
467 | + */ |
|
468 | + public function get_total() { |
|
469 | + return $this->total_customers; |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Parse and sanitize 'orderby' keys passed to the customers query. |
|
474 | + * |
|
475 | + * @since 1.0.19 |
|
476 | + * |
|
477 | + * @param string $orderby Alias for the field to order by. |
|
478 | + * @param string $table The current table. |
|
479 | + * @return string Value to use in the ORDER clause, if `$orderby` is valid. |
|
480 | + */ |
|
481 | + protected function parse_orderby( $orderby, $table ) { |
|
482 | + |
|
483 | + $_orderby = ''; |
|
484 | + if ( in_array( $orderby, array_keys( GetPaid_Customer_Data_Store::get_database_fields() ), true ) ) { |
|
485 | + $_orderby = "$table.`$orderby`"; |
|
486 | + } elseif ( 'id' === strtolower( $orderby ) ) { |
|
487 | + $_orderby = "$table.id"; |
|
488 | + } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { |
|
489 | + $include = wp_parse_id_list( $this->query_vars['include'] ); |
|
490 | + $include_sql = implode( ',', $include ); |
|
491 | + $_orderby = "FIELD( $table.id, $include_sql )"; |
|
492 | + } |
|
493 | + |
|
494 | + return $_orderby; |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. |
|
499 | + * |
|
500 | + * @since 1.0.19 |
|
501 | + * |
|
502 | + * @param string $order The 'order' query variable. |
|
503 | + * @return string The sanitized 'order' query variable. |
|
504 | + */ |
|
505 | + protected function parse_order( $order ) { |
|
506 | + if ( ! is_string( $order ) || empty( $order ) ) { |
|
507 | + return 'DESC'; |
|
508 | + } |
|
509 | + |
|
510 | + if ( 'ASC' === strtoupper( $order ) ) { |
|
511 | + return 'ASC'; |
|
512 | + } else { |
|
513 | + return 'DESC'; |
|
514 | + } |
|
515 | + } |
|
516 | 516 | |
517 | 517 | } |
@@ -20,402 +20,402 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class GetPaid_Installer { |
22 | 22 | |
23 | - /** |
|
24 | - * Upgrades the install. |
|
25 | - * |
|
26 | - * @param string $upgrade_from The current invoicing version. |
|
27 | - */ |
|
28 | - public function upgrade_db( $upgrade_from ) { |
|
29 | - |
|
30 | - // Save the current invoicing version. |
|
31 | - update_option( 'wpinv_version', WPINV_VERSION ); |
|
32 | - |
|
33 | - // Setup the invoice Custom Post Type. |
|
34 | - GetPaid_Post_Types::register_post_types(); |
|
35 | - |
|
36 | - // Clear the permalinks |
|
37 | - flush_rewrite_rules(); |
|
38 | - |
|
39 | - // Maybe create new/missing pages. |
|
40 | - $this->create_pages(); |
|
41 | - |
|
42 | - // Maybe re(add) admin capabilities. |
|
43 | - $this->add_capabilities(); |
|
44 | - |
|
45 | - // Maybe create the default payment form. |
|
46 | - wpinv_get_default_payment_form(); |
|
47 | - |
|
48 | - // Create any missing database tables. |
|
49 | - $method = "upgrade_from_$upgrade_from"; |
|
50 | - |
|
51 | - $installed = get_option( 'gepaid_installed_on' ); |
|
52 | - |
|
53 | - if ( empty( $installed ) ) { |
|
54 | - update_option( 'gepaid_installed_on', time() ); |
|
55 | - } |
|
56 | - |
|
57 | - if ( method_exists( $this, $method ) ) { |
|
58 | - $this->$method(); |
|
59 | - } |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Do a fresh install. |
|
65 | - * |
|
66 | - */ |
|
67 | - public function upgrade_from_0() { |
|
68 | - |
|
69 | - // Save default tax rates. |
|
70 | - update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Upgrade to 0.0.5 |
|
75 | - * |
|
76 | - */ |
|
77 | - public function upgrade_from_004() { |
|
78 | - global $wpdb; |
|
79 | - |
|
80 | - // Invoices. |
|
81 | - $results = $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
82 | - if ( ! empty( $results ) ) { |
|
83 | - $wpdb->query( "UPDATE {$wpdb->posts} SET post_status = CONCAT( 'wpi-', post_status ) WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
84 | - |
|
85 | - // Clean post cache |
|
86 | - foreach ( $results as $row ) { |
|
87 | - clean_post_cache( $row->ID ); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - // Item meta key changes |
|
92 | - $query = 'SELECT DISTINCT post_id FROM ' . $wpdb->postmeta . " WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id', '_wpinv_cpt_name', '_wpinv_cpt_singular_name' )"; |
|
93 | - $results = $wpdb->get_results( $query ); |
|
94 | - |
|
95 | - if ( ! empty( $results ) ) { |
|
96 | - $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" ); |
|
97 | - $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" ); |
|
98 | - $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" ); |
|
99 | - |
|
100 | - foreach ( $results as $row ) { |
|
101 | - clean_post_cache( $row->post_id ); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - $this->upgrade_from_118(); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Upgrade to version 2.0.0. |
|
110 | - * |
|
111 | - */ |
|
112 | - public function upgrade_from_118() { |
|
113 | - $this->migrate_old_invoices(); |
|
114 | - $this->upgrade_from_279(); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Upgrade to version 2.0.0. |
|
119 | - * |
|
120 | - */ |
|
121 | - public function upgrade_from_279() { |
|
122 | - self::migrate_old_customers(); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Give administrators the capability to manage GetPaid. |
|
127 | - * |
|
128 | - */ |
|
129 | - public function add_capabilities() { |
|
130 | - $GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Retreives GetPaid pages. |
|
135 | - * |
|
136 | - */ |
|
137 | - public static function get_pages() { |
|
138 | - |
|
139 | - return apply_filters( |
|
140 | - 'wpinv_create_pages', |
|
141 | - array( |
|
142 | - |
|
143 | - // Checkout page. |
|
144 | - 'checkout_page' => array( |
|
145 | - 'name' => _x( 'gp-checkout', 'Page slug', 'invoicing' ), |
|
146 | - 'title' => _x( 'Checkout', 'Page title', 'invoicing' ), |
|
147 | - 'content' => ' |
|
23 | + /** |
|
24 | + * Upgrades the install. |
|
25 | + * |
|
26 | + * @param string $upgrade_from The current invoicing version. |
|
27 | + */ |
|
28 | + public function upgrade_db( $upgrade_from ) { |
|
29 | + |
|
30 | + // Save the current invoicing version. |
|
31 | + update_option( 'wpinv_version', WPINV_VERSION ); |
|
32 | + |
|
33 | + // Setup the invoice Custom Post Type. |
|
34 | + GetPaid_Post_Types::register_post_types(); |
|
35 | + |
|
36 | + // Clear the permalinks |
|
37 | + flush_rewrite_rules(); |
|
38 | + |
|
39 | + // Maybe create new/missing pages. |
|
40 | + $this->create_pages(); |
|
41 | + |
|
42 | + // Maybe re(add) admin capabilities. |
|
43 | + $this->add_capabilities(); |
|
44 | + |
|
45 | + // Maybe create the default payment form. |
|
46 | + wpinv_get_default_payment_form(); |
|
47 | + |
|
48 | + // Create any missing database tables. |
|
49 | + $method = "upgrade_from_$upgrade_from"; |
|
50 | + |
|
51 | + $installed = get_option( 'gepaid_installed_on' ); |
|
52 | + |
|
53 | + if ( empty( $installed ) ) { |
|
54 | + update_option( 'gepaid_installed_on', time() ); |
|
55 | + } |
|
56 | + |
|
57 | + if ( method_exists( $this, $method ) ) { |
|
58 | + $this->$method(); |
|
59 | + } |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Do a fresh install. |
|
65 | + * |
|
66 | + */ |
|
67 | + public function upgrade_from_0() { |
|
68 | + |
|
69 | + // Save default tax rates. |
|
70 | + update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) ); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Upgrade to 0.0.5 |
|
75 | + * |
|
76 | + */ |
|
77 | + public function upgrade_from_004() { |
|
78 | + global $wpdb; |
|
79 | + |
|
80 | + // Invoices. |
|
81 | + $results = $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
82 | + if ( ! empty( $results ) ) { |
|
83 | + $wpdb->query( "UPDATE {$wpdb->posts} SET post_status = CONCAT( 'wpi-', post_status ) WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" ); |
|
84 | + |
|
85 | + // Clean post cache |
|
86 | + foreach ( $results as $row ) { |
|
87 | + clean_post_cache( $row->ID ); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + // Item meta key changes |
|
92 | + $query = 'SELECT DISTINCT post_id FROM ' . $wpdb->postmeta . " WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id', '_wpinv_cpt_name', '_wpinv_cpt_singular_name' )"; |
|
93 | + $results = $wpdb->get_results( $query ); |
|
94 | + |
|
95 | + if ( ! empty( $results ) ) { |
|
96 | + $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" ); |
|
97 | + $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" ); |
|
98 | + $wpdb->query( 'UPDATE ' . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" ); |
|
99 | + |
|
100 | + foreach ( $results as $row ) { |
|
101 | + clean_post_cache( $row->post_id ); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + $this->upgrade_from_118(); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Upgrade to version 2.0.0. |
|
110 | + * |
|
111 | + */ |
|
112 | + public function upgrade_from_118() { |
|
113 | + $this->migrate_old_invoices(); |
|
114 | + $this->upgrade_from_279(); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Upgrade to version 2.0.0. |
|
119 | + * |
|
120 | + */ |
|
121 | + public function upgrade_from_279() { |
|
122 | + self::migrate_old_customers(); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Give administrators the capability to manage GetPaid. |
|
127 | + * |
|
128 | + */ |
|
129 | + public function add_capabilities() { |
|
130 | + $GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Retreives GetPaid pages. |
|
135 | + * |
|
136 | + */ |
|
137 | + public static function get_pages() { |
|
138 | + |
|
139 | + return apply_filters( |
|
140 | + 'wpinv_create_pages', |
|
141 | + array( |
|
142 | + |
|
143 | + // Checkout page. |
|
144 | + 'checkout_page' => array( |
|
145 | + 'name' => _x( 'gp-checkout', 'Page slug', 'invoicing' ), |
|
146 | + 'title' => _x( 'Checkout', 'Page title', 'invoicing' ), |
|
147 | + 'content' => ' |
|
148 | 148 | <!-- wp:shortcode --> |
149 | 149 | [wpinv_checkout] |
150 | 150 | <!-- /wp:shortcode --> |
151 | 151 | ', |
152 | - 'parent' => '', |
|
153 | - ), |
|
154 | - |
|
155 | - // Invoice history page. |
|
156 | - 'invoice_history_page' => array( |
|
157 | - 'name' => _x( 'gp-invoices', 'Page slug', 'invoicing' ), |
|
158 | - 'title' => _x( 'My Invoices', 'Page title', 'invoicing' ), |
|
159 | - 'content' => ' |
|
152 | + 'parent' => '', |
|
153 | + ), |
|
154 | + |
|
155 | + // Invoice history page. |
|
156 | + 'invoice_history_page' => array( |
|
157 | + 'name' => _x( 'gp-invoices', 'Page slug', 'invoicing' ), |
|
158 | + 'title' => _x( 'My Invoices', 'Page title', 'invoicing' ), |
|
159 | + 'content' => ' |
|
160 | 160 | <!-- wp:shortcode --> |
161 | 161 | [wpinv_history] |
162 | 162 | <!-- /wp:shortcode --> |
163 | 163 | ', |
164 | - 'parent' => '', |
|
165 | - ), |
|
166 | - |
|
167 | - // Success page content. |
|
168 | - 'success_page' => array( |
|
169 | - 'name' => _x( 'gp-receipt', 'Page slug', 'invoicing' ), |
|
170 | - 'title' => _x( 'Payment Confirmation', 'Page title', 'invoicing' ), |
|
171 | - 'content' => ' |
|
164 | + 'parent' => '', |
|
165 | + ), |
|
166 | + |
|
167 | + // Success page content. |
|
168 | + 'success_page' => array( |
|
169 | + 'name' => _x( 'gp-receipt', 'Page slug', 'invoicing' ), |
|
170 | + 'title' => _x( 'Payment Confirmation', 'Page title', 'invoicing' ), |
|
171 | + 'content' => ' |
|
172 | 172 | <!-- wp:shortcode --> |
173 | 173 | [wpinv_receipt] |
174 | 174 | <!-- /wp:shortcode --> |
175 | 175 | ', |
176 | - 'parent' => 'gp-checkout', |
|
177 | - ), |
|
178 | - |
|
179 | - // Failure page content. |
|
180 | - 'failure_page' => array( |
|
181 | - 'name' => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ), |
|
182 | - 'title' => _x( 'Transaction Failed', 'Page title', 'invoicing' ), |
|
183 | - 'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ), |
|
184 | - 'parent' => 'gp-checkout', |
|
185 | - ), |
|
186 | - |
|
187 | - // Subscriptions history page. |
|
188 | - 'invoice_subscription_page' => array( |
|
189 | - 'name' => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ), |
|
190 | - 'title' => _x( 'My Subscriptions', 'Page title', 'invoicing' ), |
|
191 | - 'content' => ' |
|
176 | + 'parent' => 'gp-checkout', |
|
177 | + ), |
|
178 | + |
|
179 | + // Failure page content. |
|
180 | + 'failure_page' => array( |
|
181 | + 'name' => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ), |
|
182 | + 'title' => _x( 'Transaction Failed', 'Page title', 'invoicing' ), |
|
183 | + 'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ), |
|
184 | + 'parent' => 'gp-checkout', |
|
185 | + ), |
|
186 | + |
|
187 | + // Subscriptions history page. |
|
188 | + 'invoice_subscription_page' => array( |
|
189 | + 'name' => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ), |
|
190 | + 'title' => _x( 'My Subscriptions', 'Page title', 'invoicing' ), |
|
191 | + 'content' => ' |
|
192 | 192 | <!-- wp:shortcode --> |
193 | 193 | [wpinv_subscriptions] |
194 | 194 | <!-- /wp:shortcode --> |
195 | 195 | ', |
196 | - 'parent' => '', |
|
197 | - ), |
|
198 | - |
|
199 | - ) |
|
200 | - ); |
|
201 | - |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Re-create GetPaid pages. |
|
206 | - * |
|
207 | - */ |
|
208 | - public function create_pages() { |
|
209 | - |
|
210 | - foreach ( self::get_pages() as $key => $page ) { |
|
211 | - wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] ); |
|
212 | - } |
|
213 | - |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Migrates old invoices to new invoices. |
|
218 | - * |
|
219 | - */ |
|
220 | - public function migrate_old_invoices() { |
|
221 | - global $wpdb; |
|
222 | - |
|
223 | - $invoices_table = $wpdb->prefix . 'getpaid_invoices'; |
|
224 | - $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
225 | - $migrated = $wpdb->get_col( "SELECT post_id FROM $invoices_table" ); |
|
226 | - $invoices = array_unique( |
|
227 | - get_posts( |
|
228 | - array( |
|
229 | - 'post_type' => array( 'wpi_invoice', 'wpi_quote' ), |
|
230 | - 'posts_per_page' => -1, |
|
231 | - 'fields' => 'ids', |
|
232 | - 'post_status' => array_keys( get_post_stati() ), |
|
233 | - 'exclude' => (array) $migrated, |
|
234 | - ) |
|
235 | - ) |
|
236 | - ); |
|
237 | - |
|
238 | - // Abort if we do not have any invoices. |
|
239 | - if ( empty( $invoices ) ) { |
|
240 | - return; |
|
241 | - } |
|
242 | - |
|
243 | - require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php'; |
|
244 | - |
|
245 | - $invoice_rows = array(); |
|
246 | - foreach ( $invoices as $invoice ) { |
|
247 | - |
|
248 | - $invoice = new WPInv_Legacy_Invoice( $invoice ); |
|
249 | - |
|
250 | - if ( empty( $invoice->ID ) ) { |
|
251 | - return; |
|
252 | - } |
|
253 | - |
|
254 | - $fields = array( |
|
255 | - 'post_id' => $invoice->ID, |
|
256 | - 'number' => $invoice->get_number(), |
|
257 | - 'key' => $invoice->get_key(), |
|
258 | - 'type' => str_replace( 'wpi_', '', $invoice->post_type ), |
|
259 | - 'mode' => $invoice->mode, |
|
260 | - 'user_ip' => $invoice->get_ip(), |
|
261 | - 'first_name' => $invoice->get_first_name(), |
|
262 | - 'last_name' => $invoice->get_last_name(), |
|
263 | - 'address' => $invoice->get_address(), |
|
264 | - 'city' => $invoice->city, |
|
265 | - 'state' => $invoice->state, |
|
266 | - 'country' => $invoice->country, |
|
267 | - 'zip' => $invoice->zip, |
|
268 | - 'adddress_confirmed' => (int) $invoice->adddress_confirmed, |
|
269 | - 'gateway' => $invoice->get_gateway(), |
|
270 | - 'transaction_id' => $invoice->get_transaction_id(), |
|
271 | - 'currency' => $invoice->get_currency(), |
|
272 | - 'subtotal' => $invoice->get_subtotal(), |
|
273 | - 'tax' => $invoice->get_tax(), |
|
274 | - 'fees_total' => $invoice->get_fees_total(), |
|
275 | - 'total' => $invoice->get_total(), |
|
276 | - 'discount' => $invoice->get_discount(), |
|
277 | - 'discount_code' => $invoice->get_discount_code(), |
|
278 | - 'disable_taxes' => $invoice->disable_taxes, |
|
279 | - 'due_date' => $invoice->get_due_date(), |
|
280 | - 'completed_date' => $invoice->get_completed_date(), |
|
281 | - 'company' => $invoice->company, |
|
282 | - 'vat_number' => $invoice->vat_number, |
|
283 | - 'vat_rate' => $invoice->vat_rate, |
|
284 | - 'custom_meta' => $invoice->payment_meta, |
|
285 | - ); |
|
286 | - |
|
287 | - foreach ( $fields as $key => $val ) { |
|
288 | - if ( is_null( $val ) ) { |
|
289 | - $val = ''; |
|
290 | - } |
|
291 | - $val = maybe_serialize( $val ); |
|
292 | - $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
293 | - } |
|
294 | - |
|
295 | - $fields = implode( ', ', $fields ); |
|
296 | - $invoice_rows[] = "($fields)"; |
|
297 | - |
|
298 | - $item_rows = array(); |
|
299 | - $item_columns = array(); |
|
300 | - foreach ( $invoice->get_cart_details() as $details ) { |
|
301 | - $fields = array( |
|
302 | - 'post_id' => $invoice->ID, |
|
303 | - 'item_id' => $details['id'], |
|
304 | - 'item_name' => $details['name'], |
|
305 | - 'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'], |
|
306 | - 'vat_rate' => $details['vat_rate'], |
|
307 | - 'vat_class' => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'], |
|
308 | - 'tax' => $details['tax'], |
|
309 | - 'item_price' => $details['item_price'], |
|
310 | - 'custom_price' => $details['custom_price'], |
|
311 | - 'quantity' => $details['quantity'], |
|
312 | - 'discount' => $details['discount'], |
|
313 | - 'subtotal' => $details['subtotal'], |
|
314 | - 'price' => $details['price'], |
|
315 | - 'meta' => $details['meta'], |
|
316 | - 'fees' => $details['fees'], |
|
317 | - ); |
|
318 | - |
|
319 | - $item_columns = array_keys( $fields ); |
|
320 | - |
|
321 | - foreach ( $fields as $key => $val ) { |
|
322 | - if ( is_null( $val ) ) { |
|
323 | - $val = ''; |
|
324 | - } |
|
325 | - $val = maybe_serialize( $val ); |
|
326 | - $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
327 | - } |
|
328 | - |
|
329 | - $fields = implode( ', ', $fields ); |
|
330 | - $item_rows[] = "($fields)"; |
|
331 | - } |
|
332 | - |
|
333 | - $item_rows = implode( ', ', $item_rows ); |
|
334 | - $item_columns = implode( ', ', $item_columns ); |
|
335 | - $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" ); |
|
336 | - } |
|
337 | - |
|
338 | - if ( empty( $invoice_rows ) ) { |
|
339 | - return; |
|
340 | - } |
|
341 | - |
|
342 | - $invoice_rows = implode( ', ', $invoice_rows ); |
|
343 | - $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" ); |
|
344 | - |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Migrates old customers to new table. |
|
349 | - * |
|
350 | - */ |
|
351 | - public static function migrate_old_customers() { |
|
352 | - global $wpdb; |
|
353 | - |
|
354 | - // Fetch post_id from $wpdb->prefix . 'getpaid_invoices' where customer_id = 0 or null. |
|
355 | - $invoice_ids = $wpdb->get_col( "SELECT post_id FROM {$wpdb->prefix}getpaid_invoices WHERE customer_id = 0 OR customer_id IS NULL" ); |
|
356 | - |
|
357 | - foreach ( $invoice_ids as $invoice_id ) { |
|
358 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
359 | - |
|
360 | - if ( empty( $invoice ) ) { |
|
361 | - continue; |
|
362 | - } |
|
363 | - |
|
364 | - // Fetch customer from the user ID. |
|
365 | - $user_id = $invoice->get_user_id(); |
|
366 | - |
|
367 | - if ( empty( $user_id ) ) { |
|
368 | - continue; |
|
369 | - } |
|
370 | - |
|
371 | - $customer = getpaid_get_customer_by_user_id( $user_id ); |
|
372 | - |
|
373 | - // Create if not exists. |
|
374 | - if ( empty( $customer ) ) { |
|
375 | - $customer = new GetPaid_Customer( 0 ); |
|
376 | - $customer->clone_user( $user_id ); |
|
377 | - $customer->save(); |
|
378 | - } |
|
379 | - |
|
380 | - $invoice->set_customer_id( $customer->get_id() ); |
|
381 | - $invoice->save(); |
|
382 | - } |
|
383 | - |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Migrates old invoices to new invoices. |
|
388 | - * |
|
389 | - */ |
|
390 | - public static function rename_gateways_label() { |
|
391 | - global $wpdb; |
|
392 | - |
|
393 | - foreach ( array_keys( wpinv_get_payment_gateways() ) as $gateway ) { |
|
394 | - |
|
395 | - $wpdb->update( |
|
396 | - $wpdb->prefix . 'getpaid_invoices', |
|
397 | - array( 'gateway' => $gateway ), |
|
398 | - array( 'gateway' => wpinv_get_gateway_admin_label( $gateway ) ), |
|
399 | - '%s', |
|
400 | - '%s' |
|
401 | - ); |
|
402 | - |
|
403 | - } |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Returns the DB schema. |
|
408 | - * |
|
409 | - */ |
|
410 | - public static function get_db_schema() { |
|
411 | - global $wpdb; |
|
412 | - |
|
413 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
414 | - |
|
415 | - $charset_collate = $wpdb->get_charset_collate(); |
|
196 | + 'parent' => '', |
|
197 | + ), |
|
198 | + |
|
199 | + ) |
|
200 | + ); |
|
201 | + |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Re-create GetPaid pages. |
|
206 | + * |
|
207 | + */ |
|
208 | + public function create_pages() { |
|
209 | + |
|
210 | + foreach ( self::get_pages() as $key => $page ) { |
|
211 | + wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] ); |
|
212 | + } |
|
213 | + |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Migrates old invoices to new invoices. |
|
218 | + * |
|
219 | + */ |
|
220 | + public function migrate_old_invoices() { |
|
221 | + global $wpdb; |
|
222 | + |
|
223 | + $invoices_table = $wpdb->prefix . 'getpaid_invoices'; |
|
224 | + $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
225 | + $migrated = $wpdb->get_col( "SELECT post_id FROM $invoices_table" ); |
|
226 | + $invoices = array_unique( |
|
227 | + get_posts( |
|
228 | + array( |
|
229 | + 'post_type' => array( 'wpi_invoice', 'wpi_quote' ), |
|
230 | + 'posts_per_page' => -1, |
|
231 | + 'fields' => 'ids', |
|
232 | + 'post_status' => array_keys( get_post_stati() ), |
|
233 | + 'exclude' => (array) $migrated, |
|
234 | + ) |
|
235 | + ) |
|
236 | + ); |
|
237 | + |
|
238 | + // Abort if we do not have any invoices. |
|
239 | + if ( empty( $invoices ) ) { |
|
240 | + return; |
|
241 | + } |
|
242 | + |
|
243 | + require_once WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php'; |
|
244 | + |
|
245 | + $invoice_rows = array(); |
|
246 | + foreach ( $invoices as $invoice ) { |
|
247 | + |
|
248 | + $invoice = new WPInv_Legacy_Invoice( $invoice ); |
|
249 | + |
|
250 | + if ( empty( $invoice->ID ) ) { |
|
251 | + return; |
|
252 | + } |
|
253 | + |
|
254 | + $fields = array( |
|
255 | + 'post_id' => $invoice->ID, |
|
256 | + 'number' => $invoice->get_number(), |
|
257 | + 'key' => $invoice->get_key(), |
|
258 | + 'type' => str_replace( 'wpi_', '', $invoice->post_type ), |
|
259 | + 'mode' => $invoice->mode, |
|
260 | + 'user_ip' => $invoice->get_ip(), |
|
261 | + 'first_name' => $invoice->get_first_name(), |
|
262 | + 'last_name' => $invoice->get_last_name(), |
|
263 | + 'address' => $invoice->get_address(), |
|
264 | + 'city' => $invoice->city, |
|
265 | + 'state' => $invoice->state, |
|
266 | + 'country' => $invoice->country, |
|
267 | + 'zip' => $invoice->zip, |
|
268 | + 'adddress_confirmed' => (int) $invoice->adddress_confirmed, |
|
269 | + 'gateway' => $invoice->get_gateway(), |
|
270 | + 'transaction_id' => $invoice->get_transaction_id(), |
|
271 | + 'currency' => $invoice->get_currency(), |
|
272 | + 'subtotal' => $invoice->get_subtotal(), |
|
273 | + 'tax' => $invoice->get_tax(), |
|
274 | + 'fees_total' => $invoice->get_fees_total(), |
|
275 | + 'total' => $invoice->get_total(), |
|
276 | + 'discount' => $invoice->get_discount(), |
|
277 | + 'discount_code' => $invoice->get_discount_code(), |
|
278 | + 'disable_taxes' => $invoice->disable_taxes, |
|
279 | + 'due_date' => $invoice->get_due_date(), |
|
280 | + 'completed_date' => $invoice->get_completed_date(), |
|
281 | + 'company' => $invoice->company, |
|
282 | + 'vat_number' => $invoice->vat_number, |
|
283 | + 'vat_rate' => $invoice->vat_rate, |
|
284 | + 'custom_meta' => $invoice->payment_meta, |
|
285 | + ); |
|
286 | + |
|
287 | + foreach ( $fields as $key => $val ) { |
|
288 | + if ( is_null( $val ) ) { |
|
289 | + $val = ''; |
|
290 | + } |
|
291 | + $val = maybe_serialize( $val ); |
|
292 | + $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
293 | + } |
|
294 | + |
|
295 | + $fields = implode( ', ', $fields ); |
|
296 | + $invoice_rows[] = "($fields)"; |
|
297 | + |
|
298 | + $item_rows = array(); |
|
299 | + $item_columns = array(); |
|
300 | + foreach ( $invoice->get_cart_details() as $details ) { |
|
301 | + $fields = array( |
|
302 | + 'post_id' => $invoice->ID, |
|
303 | + 'item_id' => $details['id'], |
|
304 | + 'item_name' => $details['name'], |
|
305 | + 'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'], |
|
306 | + 'vat_rate' => $details['vat_rate'], |
|
307 | + 'vat_class' => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'], |
|
308 | + 'tax' => $details['tax'], |
|
309 | + 'item_price' => $details['item_price'], |
|
310 | + 'custom_price' => $details['custom_price'], |
|
311 | + 'quantity' => $details['quantity'], |
|
312 | + 'discount' => $details['discount'], |
|
313 | + 'subtotal' => $details['subtotal'], |
|
314 | + 'price' => $details['price'], |
|
315 | + 'meta' => $details['meta'], |
|
316 | + 'fees' => $details['fees'], |
|
317 | + ); |
|
318 | + |
|
319 | + $item_columns = array_keys( $fields ); |
|
320 | + |
|
321 | + foreach ( $fields as $key => $val ) { |
|
322 | + if ( is_null( $val ) ) { |
|
323 | + $val = ''; |
|
324 | + } |
|
325 | + $val = maybe_serialize( $val ); |
|
326 | + $fields[ $key ] = $wpdb->prepare( '%s', $val ); |
|
327 | + } |
|
328 | + |
|
329 | + $fields = implode( ', ', $fields ); |
|
330 | + $item_rows[] = "($fields)"; |
|
331 | + } |
|
332 | + |
|
333 | + $item_rows = implode( ', ', $item_rows ); |
|
334 | + $item_columns = implode( ', ', $item_columns ); |
|
335 | + $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" ); |
|
336 | + } |
|
337 | + |
|
338 | + if ( empty( $invoice_rows ) ) { |
|
339 | + return; |
|
340 | + } |
|
341 | + |
|
342 | + $invoice_rows = implode( ', ', $invoice_rows ); |
|
343 | + $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" ); |
|
344 | + |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Migrates old customers to new table. |
|
349 | + * |
|
350 | + */ |
|
351 | + public static function migrate_old_customers() { |
|
352 | + global $wpdb; |
|
353 | + |
|
354 | + // Fetch post_id from $wpdb->prefix . 'getpaid_invoices' where customer_id = 0 or null. |
|
355 | + $invoice_ids = $wpdb->get_col( "SELECT post_id FROM {$wpdb->prefix}getpaid_invoices WHERE customer_id = 0 OR customer_id IS NULL" ); |
|
356 | + |
|
357 | + foreach ( $invoice_ids as $invoice_id ) { |
|
358 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
359 | + |
|
360 | + if ( empty( $invoice ) ) { |
|
361 | + continue; |
|
362 | + } |
|
363 | + |
|
364 | + // Fetch customer from the user ID. |
|
365 | + $user_id = $invoice->get_user_id(); |
|
366 | + |
|
367 | + if ( empty( $user_id ) ) { |
|
368 | + continue; |
|
369 | + } |
|
370 | + |
|
371 | + $customer = getpaid_get_customer_by_user_id( $user_id ); |
|
372 | + |
|
373 | + // Create if not exists. |
|
374 | + if ( empty( $customer ) ) { |
|
375 | + $customer = new GetPaid_Customer( 0 ); |
|
376 | + $customer->clone_user( $user_id ); |
|
377 | + $customer->save(); |
|
378 | + } |
|
379 | + |
|
380 | + $invoice->set_customer_id( $customer->get_id() ); |
|
381 | + $invoice->save(); |
|
382 | + } |
|
383 | + |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Migrates old invoices to new invoices. |
|
388 | + * |
|
389 | + */ |
|
390 | + public static function rename_gateways_label() { |
|
391 | + global $wpdb; |
|
392 | + |
|
393 | + foreach ( array_keys( wpinv_get_payment_gateways() ) as $gateway ) { |
|
394 | + |
|
395 | + $wpdb->update( |
|
396 | + $wpdb->prefix . 'getpaid_invoices', |
|
397 | + array( 'gateway' => $gateway ), |
|
398 | + array( 'gateway' => wpinv_get_gateway_admin_label( $gateway ) ), |
|
399 | + '%s', |
|
400 | + '%s' |
|
401 | + ); |
|
402 | + |
|
403 | + } |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Returns the DB schema. |
|
408 | + * |
|
409 | + */ |
|
410 | + public static function get_db_schema() { |
|
411 | + global $wpdb; |
|
412 | + |
|
413 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
414 | + |
|
415 | + $charset_collate = $wpdb->get_charset_collate(); |
|
416 | 416 | |
417 | - // Subscriptions. |
|
418 | - $schema = "CREATE TABLE {$wpdb->prefix}wpinv_subscriptions ( |
|
417 | + // Subscriptions. |
|
418 | + $schema = "CREATE TABLE {$wpdb->prefix}wpinv_subscriptions ( |
|
419 | 419 | id bigint(20) unsigned NOT NULL auto_increment, |
420 | 420 | customer_id bigint(20) NOT NULL, |
421 | 421 | frequency int(11) NOT NULL DEFAULT '1', |
@@ -438,8 +438,8 @@ discard block |
||
438 | 438 | KEY customer_and_status (customer_id, status) |
439 | 439 | ) $charset_collate;"; |
440 | 440 | |
441 | - // Invoices. |
|
442 | - $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_invoices ( |
|
441 | + // Invoices. |
|
442 | + $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_invoices ( |
|
443 | 443 | post_id BIGINT(20) NOT NULL, |
444 | 444 | customer_id BIGINT(20) NOT NULL DEFAULT 0, |
445 | 445 | `number` VARCHAR(100), |
@@ -476,8 +476,8 @@ discard block |
||
476 | 476 | KEY invoice_key (invoice_key) |
477 | 477 | ) $charset_collate;"; |
478 | 478 | |
479 | - // Invoice items. |
|
480 | - $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_invoice_items ( |
|
479 | + // Invoice items. |
|
480 | + $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_invoice_items ( |
|
481 | 481 | ID BIGINT(20) NOT NULL AUTO_INCREMENT, |
482 | 482 | post_id BIGINT(20) NOT NULL, |
483 | 483 | item_id BIGINT(20) NOT NULL, |
@@ -499,8 +499,8 @@ discard block |
||
499 | 499 | KEY post_id (post_id) |
500 | 500 | ) $charset_collate;"; |
501 | 501 | |
502 | - // Customers. |
|
503 | - $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_customers ( |
|
502 | + // Customers. |
|
503 | + $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_customers ( |
|
504 | 504 | id BIGINT(20) NOT NULL AUTO_INCREMENT, |
505 | 505 | user_id BIGINT(20) NOT NULL, |
506 | 506 | email VARCHAR(100) NOT NULL, |
@@ -510,38 +510,38 @@ discard block |
||
510 | 510 | purchase_count BIGINT(20) NOT NULL DEFAULT 0, |
511 | 511 | "; |
512 | 512 | |
513 | - // Add address fields. |
|
514 | - foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
515 | - // Skip id, user_id and email. |
|
516 | - if ( in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
517 | - continue; |
|
518 | - } |
|
519 | - |
|
520 | - $field = sanitize_key( $field ); |
|
521 | - $length = 100; |
|
522 | - $default = ''; |
|
523 | - |
|
524 | - // Country. |
|
525 | - if ( 'country' === $field ) { |
|
526 | - $length = 2; |
|
527 | - $default = wpinv_get_default_country(); |
|
528 | - } |
|
529 | - |
|
530 | - // State. |
|
531 | - if ( 'state' === $field ) { |
|
532 | - $default = wpinv_get_default_state(); |
|
533 | - } |
|
534 | - |
|
535 | - // Phone, zip. |
|
536 | - if ( in_array( $field, array( 'phone', 'zip' ), true ) ) { |
|
537 | - $length = 20; |
|
538 | - } |
|
539 | - |
|
540 | - $schema .= "`$field` VARCHAR($length) NOT NULL DEFAULT '$default', |
|
513 | + // Add address fields. |
|
514 | + foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
|
515 | + // Skip id, user_id and email. |
|
516 | + if ( in_array( $field, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid' ), true ) ) { |
|
517 | + continue; |
|
518 | + } |
|
519 | + |
|
520 | + $field = sanitize_key( $field ); |
|
521 | + $length = 100; |
|
522 | + $default = ''; |
|
523 | + |
|
524 | + // Country. |
|
525 | + if ( 'country' === $field ) { |
|
526 | + $length = 2; |
|
527 | + $default = wpinv_get_default_country(); |
|
528 | + } |
|
529 | + |
|
530 | + // State. |
|
531 | + if ( 'state' === $field ) { |
|
532 | + $default = wpinv_get_default_state(); |
|
533 | + } |
|
534 | + |
|
535 | + // Phone, zip. |
|
536 | + if ( in_array( $field, array( 'phone', 'zip' ), true ) ) { |
|
537 | + $length = 20; |
|
538 | + } |
|
539 | + |
|
540 | + $schema .= "`$field` VARCHAR($length) NOT NULL DEFAULT '$default', |
|
541 | 541 | "; |
542 | - } |
|
542 | + } |
|
543 | 543 | |
544 | - $schema .= "date_created DATETIME NOT NULL, |
|
544 | + $schema .= "date_created DATETIME NOT NULL, |
|
545 | 545 | date_modified DATETIME NOT NULL, |
546 | 546 | uuid VARCHAR(100) NOT NULL, |
547 | 547 | PRIMARY KEY (id), |
@@ -549,8 +549,8 @@ discard block |
||
549 | 549 | KEY email (email) |
550 | 550 | ) $charset_collate;"; |
551 | 551 | |
552 | - // Customer meta. |
|
553 | - $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_customer_meta ( |
|
552 | + // Customer meta. |
|
553 | + $schema .= "CREATE TABLE {$wpdb->prefix}getpaid_customer_meta ( |
|
554 | 554 | meta_id BIGINT(20) NOT NULL AUTO_INCREMENT, |
555 | 555 | customer_id BIGINT(20) NOT NULL, |
556 | 556 | meta_key VARCHAR(255) NOT NULL, |
@@ -560,55 +560,55 @@ discard block |
||
560 | 560 | KEY meta_key (meta_key(191)) |
561 | 561 | ) $charset_collate;"; |
562 | 562 | |
563 | - return $schema; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Checks if the db schema is up to date. |
|
568 | - * |
|
569 | - * @return bool |
|
570 | - */ |
|
571 | - public static function is_db_schema_up_to_date() { |
|
572 | - return md5( self::get_db_schema() ) === get_option( 'getpaid_db_schema' ); |
|
573 | - } |
|
574 | - |
|
575 | - /** |
|
576 | - * Set up the database tables which the plugin needs to function. |
|
577 | - */ |
|
578 | - public static function create_db_tables() { |
|
579 | - global $wpdb; |
|
580 | - |
|
581 | - $wpdb->hide_errors(); |
|
582 | - |
|
583 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
584 | - |
|
585 | - $schema = self::get_db_schema(); |
|
586 | - |
|
587 | - // If invoices table exists, rename key to invoice_key. |
|
588 | - $invoices_table = "{$wpdb->prefix}getpaid_invoices"; |
|
589 | - |
|
590 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) === $invoices_table ) { |
|
591 | - $fields = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}getpaid_invoices" ); |
|
592 | - |
|
593 | - foreach ( $fields as $field ) { |
|
594 | - if ( 'key' === $field->Field ) { |
|
595 | - $wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoices CHANGE `key` `invoice_key` VARCHAR(100)" ); |
|
596 | - break; |
|
597 | - } |
|
598 | - } |
|
599 | - } |
|
600 | - |
|
601 | - dbDelta( $schema ); |
|
602 | - wp_cache_flush(); |
|
603 | - update_option( 'getpaid_db_schema', md5( $schema ) ); |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Creates tables if schema is not up to date. |
|
608 | - */ |
|
609 | - public static function maybe_create_db_tables() { |
|
610 | - if ( ! self::is_db_schema_up_to_date() ) { |
|
611 | - self::create_db_tables(); |
|
612 | - } |
|
613 | - } |
|
563 | + return $schema; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Checks if the db schema is up to date. |
|
568 | + * |
|
569 | + * @return bool |
|
570 | + */ |
|
571 | + public static function is_db_schema_up_to_date() { |
|
572 | + return md5( self::get_db_schema() ) === get_option( 'getpaid_db_schema' ); |
|
573 | + } |
|
574 | + |
|
575 | + /** |
|
576 | + * Set up the database tables which the plugin needs to function. |
|
577 | + */ |
|
578 | + public static function create_db_tables() { |
|
579 | + global $wpdb; |
|
580 | + |
|
581 | + $wpdb->hide_errors(); |
|
582 | + |
|
583 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
584 | + |
|
585 | + $schema = self::get_db_schema(); |
|
586 | + |
|
587 | + // If invoices table exists, rename key to invoice_key. |
|
588 | + $invoices_table = "{$wpdb->prefix}getpaid_invoices"; |
|
589 | + |
|
590 | + if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}getpaid_invoices'" ) === $invoices_table ) { |
|
591 | + $fields = $wpdb->get_results( "SHOW COLUMNS FROM {$wpdb->prefix}getpaid_invoices" ); |
|
592 | + |
|
593 | + foreach ( $fields as $field ) { |
|
594 | + if ( 'key' === $field->Field ) { |
|
595 | + $wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoices CHANGE `key` `invoice_key` VARCHAR(100)" ); |
|
596 | + break; |
|
597 | + } |
|
598 | + } |
|
599 | + } |
|
600 | + |
|
601 | + dbDelta( $schema ); |
|
602 | + wp_cache_flush(); |
|
603 | + update_option( 'getpaid_db_schema', md5( $schema ) ); |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Creates tables if schema is not up to date. |
|
608 | + */ |
|
609 | + public static function maybe_create_db_tables() { |
|
610 | + if ( ! self::is_db_schema_up_to_date() ) { |
|
611 | + self::create_db_tables(); |
|
612 | + } |
|
613 | + } |
|
614 | 614 | } |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | |
12 | 12 | // Load WP_List_Table if not loaded |
13 | 13 | if ( ! class_exists( 'WP_List_Table' ) ) { |
14 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
14 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | /** |
@@ -23,303 +23,303 @@ discard block |
||
23 | 23 | */ |
24 | 24 | class WPInv_Customers_Table extends WP_List_Table { |
25 | 25 | |
26 | - /** |
|
27 | - * @var int Number of items per page |
|
28 | - * @since 1.0.19 |
|
29 | - */ |
|
30 | - public $per_page = 25; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var int Number of items |
|
34 | - * @since 1.0.19 |
|
35 | - */ |
|
36 | - public $total_count = 0; |
|
37 | - |
|
38 | - public $query; |
|
39 | - |
|
40 | - /** |
|
41 | - * Get things started |
|
42 | - * |
|
43 | - * @since 1.0.19 |
|
44 | - * @see WP_List_Table::__construct() |
|
45 | - */ |
|
46 | - public function __construct() { |
|
47 | - |
|
48 | - // Set parent defaults |
|
49 | - parent::__construct( |
|
26 | + /** |
|
27 | + * @var int Number of items per page |
|
28 | + * @since 1.0.19 |
|
29 | + */ |
|
30 | + public $per_page = 25; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var int Number of items |
|
34 | + * @since 1.0.19 |
|
35 | + */ |
|
36 | + public $total_count = 0; |
|
37 | + |
|
38 | + public $query; |
|
39 | + |
|
40 | + /** |
|
41 | + * Get things started |
|
42 | + * |
|
43 | + * @since 1.0.19 |
|
44 | + * @see WP_List_Table::__construct() |
|
45 | + */ |
|
46 | + public function __construct() { |
|
47 | + |
|
48 | + // Set parent defaults |
|
49 | + parent::__construct( |
|
50 | 50 | array( |
51 | - 'singular' => 'id', |
|
52 | - 'plural' => 'ids', |
|
53 | - 'ajax' => false, |
|
51 | + 'singular' => 'id', |
|
52 | + 'plural' => 'ids', |
|
53 | + 'ajax' => false, |
|
54 | 54 | ) |
55 | 55 | ); |
56 | 56 | |
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Gets the name of the primary column. |
|
61 | - * |
|
62 | - * @since 1.0.19 |
|
63 | - * @access protected |
|
64 | - * |
|
65 | - * @return string Name of the primary column. |
|
66 | - */ |
|
67 | - protected function get_primary_column_name() { |
|
68 | - return 'customer'; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * This function renders most of the columns in the list table. |
|
73 | - * |
|
74 | - * @since 1.0.19 |
|
75 | - * |
|
76 | - * @param GetPaid_Customer $customer |
|
77 | - * @param string $column_name The name of the column |
|
78 | - * |
|
79 | - * @return string Column Name |
|
80 | - */ |
|
81 | - public function column_default( $customer, $column_name ) { |
|
82 | - $value = esc_html( $customer->get( $column_name ) ); |
|
83 | - return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Displays the country column. |
|
88 | - * |
|
89 | - * @since 1.0.19 |
|
90 | - * |
|
91 | - * @param GetPaid_Customer $customer |
|
92 | - * |
|
93 | - * @return string Column Name |
|
94 | - */ |
|
95 | - public function column_country( $customer ) { |
|
96 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
97 | - if ( $country ) { |
|
98 | - $country = wpinv_country_name( $country ); |
|
99 | - } |
|
100 | - return esc_html( $country ); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Displays the state column. |
|
105 | - * |
|
106 | - * @since 1.0.19 |
|
107 | - * |
|
108 | - * @param GetPaid_Customer $customer |
|
109 | - * |
|
110 | - * @return string Column Name |
|
111 | - */ |
|
112 | - public function column_state( $customer ) { |
|
113 | - $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
114 | - $state = $customer->get( 'state' ); |
|
115 | - if ( $state ) { |
|
116 | - $state = wpinv_state_name( $state, $country ); |
|
117 | - } |
|
118 | - |
|
119 | - return esc_html( $state ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Displays the signup column. |
|
124 | - * |
|
125 | - * @since 1.0.19 |
|
126 | - * |
|
127 | - * @param GetPaid_Customer $customer |
|
128 | - * |
|
129 | - * @return string Column Name |
|
130 | - */ |
|
131 | - public function column_date_created( $customer ) { |
|
132 | - return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Displays the total spent column. |
|
137 | - * |
|
138 | - * @since 1.0.19 |
|
139 | - * |
|
140 | - * @param GetPaid_Customer $customer |
|
141 | - * |
|
142 | - * @return string Column Name |
|
143 | - */ |
|
144 | - public function column_purchase_value( $customer ) { |
|
145 | - return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Displays the total spent column. |
|
150 | - * |
|
151 | - * @since 1.0.19 |
|
152 | - * |
|
153 | - * @param GetPaid_Customer $customer |
|
154 | - * |
|
155 | - * @return string Column Name |
|
156 | - */ |
|
157 | - public function column_purchase_count( $customer ) { |
|
158 | - $value = $customer->get( 'purchase_count' ); |
|
159 | - $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
160 | - |
|
161 | - return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
162 | - |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Displays the customers name |
|
167 | - * |
|
168 | - * @param GetPaid_Customer $customer customer. |
|
169 | - * @return string |
|
170 | - */ |
|
171 | - public function column_customer( $customer ) { |
|
172 | - |
|
173 | - $first_name = $customer->get( 'first_name' ); |
|
174 | - $last_name = $customer->get( 'last_name' ); |
|
175 | - $email = $customer->get( 'email' ); |
|
176 | - $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
177 | - |
|
178 | - // Customer view URL. |
|
179 | - $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
180 | - $row_actions = $view_url ? $this->row_actions( |
|
181 | - array( |
|
182 | - 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
183 | - ) |
|
184 | - ) : ''; |
|
185 | - |
|
186 | - // Customer's name. |
|
187 | - $name = esc_html( trim( "$first_name $last_name" ) ); |
|
188 | - |
|
189 | - if ( ! empty( $name ) ) { |
|
190 | - $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
191 | - } |
|
192 | - |
|
193 | - $email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
194 | - |
|
195 | - return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
196 | - |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Retrieve the current page number |
|
201 | - * |
|
202 | - * @since 1.0.19 |
|
203 | - * @return int Current page number |
|
204 | - */ |
|
205 | - public function get_paged() { |
|
206 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Returns bulk actions. |
|
211 | - * |
|
212 | - * @since 1.0.19 |
|
213 | - * @return void |
|
214 | - */ |
|
215 | - public function bulk_actions( $which = '' ) { |
|
216 | - return array(); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Prepares the display query |
|
221 | - */ |
|
222 | - public function prepare_query() { |
|
223 | - |
|
224 | - // Prepare query args. |
|
225 | - $query = array( |
|
226 | - 'number' => $this->per_page, |
|
227 | - 'paged' => $this->get_paged(), |
|
228 | - ); |
|
229 | - |
|
230 | - foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
231 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | - |
|
238 | - if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239 | - $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
240 | - } |
|
241 | - |
|
242 | - // Min max. |
|
243 | - if ( '%f' === $type || '%d' === $type ) { |
|
244 | - |
|
245 | - if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246 | - $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
247 | - } |
|
248 | - |
|
249 | - if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250 | - $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
251 | - } |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - // Prepare class properties. |
|
256 | - $this->query = getpaid_get_customers( $query, 'query' ); |
|
257 | - $this->total_count = $this->query->get_total(); |
|
258 | - $this->items = $this->query->get_results(); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Setup the final data for the table |
|
263 | - * |
|
264 | - */ |
|
265 | - public function prepare_items() { |
|
266 | - |
|
267 | - $columns = $this->get_columns(); |
|
268 | - $hidden = array(); |
|
269 | - $sortable = $this->get_sortable_columns(); |
|
270 | - $this->prepare_query(); |
|
271 | - |
|
272 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
273 | - |
|
274 | - $this->set_pagination_args( |
|
275 | - array( |
|
276 | - 'total_items' => $this->total_count, |
|
277 | - 'per_page' => $this->per_page, |
|
278 | - 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
279 | - ) |
|
280 | - ); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Sortable table columns. |
|
285 | - * |
|
286 | - * @return array |
|
287 | - */ |
|
288 | - public function get_sortable_columns() { |
|
289 | - $sortable = array( |
|
290 | - 'customer' => array( 'first_name', true ), |
|
291 | - ); |
|
292 | - |
|
293 | - foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
294 | - $sortable[ $field ] = array( $field, true ); |
|
295 | - } |
|
296 | - |
|
297 | - return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Table columns |
|
302 | - * |
|
303 | - * @return array |
|
304 | - */ |
|
305 | - public function get_columns() { |
|
306 | - $columns = array( |
|
307 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
308 | - ); |
|
309 | - |
|
310 | - // Add address fields. |
|
311 | - foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
312 | - |
|
313 | - // Skip id, user_id and email. |
|
314 | - if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
315 | - $columns[ $key ] = $value; |
|
316 | - } |
|
317 | - } |
|
318 | - |
|
319 | - $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
320 | - $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
321 | - $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
322 | - |
|
323 | - return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
324 | - } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Gets the name of the primary column. |
|
61 | + * |
|
62 | + * @since 1.0.19 |
|
63 | + * @access protected |
|
64 | + * |
|
65 | + * @return string Name of the primary column. |
|
66 | + */ |
|
67 | + protected function get_primary_column_name() { |
|
68 | + return 'customer'; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * This function renders most of the columns in the list table. |
|
73 | + * |
|
74 | + * @since 1.0.19 |
|
75 | + * |
|
76 | + * @param GetPaid_Customer $customer |
|
77 | + * @param string $column_name The name of the column |
|
78 | + * |
|
79 | + * @return string Column Name |
|
80 | + */ |
|
81 | + public function column_default( $customer, $column_name ) { |
|
82 | + $value = esc_html( $customer->get( $column_name ) ); |
|
83 | + return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $customer ); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Displays the country column. |
|
88 | + * |
|
89 | + * @since 1.0.19 |
|
90 | + * |
|
91 | + * @param GetPaid_Customer $customer |
|
92 | + * |
|
93 | + * @return string Column Name |
|
94 | + */ |
|
95 | + public function column_country( $customer ) { |
|
96 | + $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
97 | + if ( $country ) { |
|
98 | + $country = wpinv_country_name( $country ); |
|
99 | + } |
|
100 | + return esc_html( $country ); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Displays the state column. |
|
105 | + * |
|
106 | + * @since 1.0.19 |
|
107 | + * |
|
108 | + * @param GetPaid_Customer $customer |
|
109 | + * |
|
110 | + * @return string Column Name |
|
111 | + */ |
|
112 | + public function column_state( $customer ) { |
|
113 | + $country = wpinv_sanitize_country( $customer->get( 'country' ) ); |
|
114 | + $state = $customer->get( 'state' ); |
|
115 | + if ( $state ) { |
|
116 | + $state = wpinv_state_name( $state, $country ); |
|
117 | + } |
|
118 | + |
|
119 | + return esc_html( $state ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Displays the signup column. |
|
124 | + * |
|
125 | + * @since 1.0.19 |
|
126 | + * |
|
127 | + * @param GetPaid_Customer $customer |
|
128 | + * |
|
129 | + * @return string Column Name |
|
130 | + */ |
|
131 | + public function column_date_created( $customer ) { |
|
132 | + return getpaid_format_date_value( $customer->get( 'date_created' ) ); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Displays the total spent column. |
|
137 | + * |
|
138 | + * @since 1.0.19 |
|
139 | + * |
|
140 | + * @param GetPaid_Customer $customer |
|
141 | + * |
|
142 | + * @return string Column Name |
|
143 | + */ |
|
144 | + public function column_purchase_value( $customer ) { |
|
145 | + return wpinv_price( (float) $customer->get( 'purchase_value' ) ); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Displays the total spent column. |
|
150 | + * |
|
151 | + * @since 1.0.19 |
|
152 | + * |
|
153 | + * @param GetPaid_Customer $customer |
|
154 | + * |
|
155 | + * @return string Column Name |
|
156 | + */ |
|
157 | + public function column_purchase_count( $customer ) { |
|
158 | + $value = $customer->get( 'purchase_count' ); |
|
159 | + $url = $customer->get( 'user_id' ) ? add_query_arg( array( 'post_type' => 'wpi_invoice', 'author' => $customer->get( 'user_id' ), ), admin_url( 'edit.php' ) ) : ''; |
|
160 | + |
|
161 | + return ( empty( $value ) || empty( $url ) ) ? (int) $value : '<a href="' . esc_url( $url ) . '">' . absint( $value ) . '</a>'; |
|
162 | + |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Displays the customers name |
|
167 | + * |
|
168 | + * @param GetPaid_Customer $customer customer. |
|
169 | + * @return string |
|
170 | + */ |
|
171 | + public function column_customer( $customer ) { |
|
172 | + |
|
173 | + $first_name = $customer->get( 'first_name' ); |
|
174 | + $last_name = $customer->get( 'last_name' ); |
|
175 | + $email = $customer->get( 'email' ); |
|
176 | + $avatar = get_avatar( $customer->get( 'user_id' ) ? $customer->get( 'user_id' ) : $email, 32 ); |
|
177 | + |
|
178 | + // Customer view URL. |
|
179 | + $view_url = $customer->get( 'user_id' ) ? esc_url( add_query_arg( 'user_id', $customer->get( 'user_id' ), admin_url( 'user-edit.php' ) ) ) : false; |
|
180 | + $row_actions = $view_url ? $this->row_actions( |
|
181 | + array( |
|
182 | + 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
183 | + ) |
|
184 | + ) : ''; |
|
185 | + |
|
186 | + // Customer's name. |
|
187 | + $name = esc_html( trim( "$first_name $last_name" ) ); |
|
188 | + |
|
189 | + if ( ! empty( $name ) ) { |
|
190 | + $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
191 | + } |
|
192 | + |
|
193 | + $email = "<div class='row-title'><a href='mailto:$email'>$email</a></div>"; |
|
194 | + |
|
195 | + return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
196 | + |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Retrieve the current page number |
|
201 | + * |
|
202 | + * @since 1.0.19 |
|
203 | + * @return int Current page number |
|
204 | + */ |
|
205 | + public function get_paged() { |
|
206 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Returns bulk actions. |
|
211 | + * |
|
212 | + * @since 1.0.19 |
|
213 | + * @return void |
|
214 | + */ |
|
215 | + public function bulk_actions( $which = '' ) { |
|
216 | + return array(); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Prepares the display query |
|
221 | + */ |
|
222 | + public function prepare_query() { |
|
223 | + |
|
224 | + // Prepare query args. |
|
225 | + $query = array( |
|
226 | + 'number' => $this->per_page, |
|
227 | + 'paged' => $this->get_paged(), |
|
228 | + ); |
|
229 | + |
|
230 | + foreach ( array( 'orderby', 'order', 's' ) as $field ) { |
|
231 | + if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
232 | + $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
237 | + |
|
238 | + if ( isset( $_GET[ $field ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
239 | + $query[ $field ] = wpinv_clean( rawurlencode_deep( $_GET[ $field ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
240 | + } |
|
241 | + |
|
242 | + // Min max. |
|
243 | + if ( '%f' === $type || '%d' === $type ) { |
|
244 | + |
|
245 | + if ( isset( $_GET[ $field . '_min' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
246 | + $query[ $field . '_min' ] = floatval( $_GET[ $field . '_min' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
247 | + } |
|
248 | + |
|
249 | + if ( isset( $_GET[ $field . '_max' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
250 | + $query[ $field . '_max' ] = floatval( $_GET[ $field . '_max' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
251 | + } |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + // Prepare class properties. |
|
256 | + $this->query = getpaid_get_customers( $query, 'query' ); |
|
257 | + $this->total_count = $this->query->get_total(); |
|
258 | + $this->items = $this->query->get_results(); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Setup the final data for the table |
|
263 | + * |
|
264 | + */ |
|
265 | + public function prepare_items() { |
|
266 | + |
|
267 | + $columns = $this->get_columns(); |
|
268 | + $hidden = array(); |
|
269 | + $sortable = $this->get_sortable_columns(); |
|
270 | + $this->prepare_query(); |
|
271 | + |
|
272 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
273 | + |
|
274 | + $this->set_pagination_args( |
|
275 | + array( |
|
276 | + 'total_items' => $this->total_count, |
|
277 | + 'per_page' => $this->per_page, |
|
278 | + 'total_pages' => ceil( $this->total_count / $this->per_page ), |
|
279 | + ) |
|
280 | + ); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Sortable table columns. |
|
285 | + * |
|
286 | + * @return array |
|
287 | + */ |
|
288 | + public function get_sortable_columns() { |
|
289 | + $sortable = array( |
|
290 | + 'customer' => array( 'first_name', true ), |
|
291 | + ); |
|
292 | + |
|
293 | + foreach ( GetPaid_Customer_Data_Store::get_database_fields() as $field => $type ) { |
|
294 | + $sortable[ $field ] = array( $field, true ); |
|
295 | + } |
|
296 | + |
|
297 | + return apply_filters( 'manage_getpaid_customers_sortable_table_columns', $sortable ); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Table columns |
|
302 | + * |
|
303 | + * @return array |
|
304 | + */ |
|
305 | + public function get_columns() { |
|
306 | + $columns = array( |
|
307 | + 'customer' => __( 'Customer', 'invoicing' ), |
|
308 | + ); |
|
309 | + |
|
310 | + // Add address fields. |
|
311 | + foreach ( getpaid_user_address_fields() as $key => $value ) { |
|
312 | + |
|
313 | + // Skip id, user_id and email. |
|
314 | + if ( ! in_array( $key, array( 'id', 'user_id', 'email', 'purchase_value', 'purchase_count', 'date_created', 'date_modified', 'uuid', 'first_name', 'last_name' ), true ) ) { |
|
315 | + $columns[ $key ] = $value; |
|
316 | + } |
|
317 | + } |
|
318 | + |
|
319 | + $columns['purchase_value'] = __( 'Total Spend', 'invoicing' ); |
|
320 | + $columns['purchase_count'] = __( 'Invoices', 'invoicing' ); |
|
321 | + $columns['date_created'] = __( 'Date created', 'invoicing' ); |
|
322 | + |
|
323 | + return apply_filters( 'manage_getpaid_customers_table_columns', $columns ); |
|
324 | + } |
|
325 | 325 | } |