1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains the main CLI class |
4
|
|
|
* |
5
|
|
|
* @since 1.0.13 |
6
|
|
|
* @package Invoicing |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
// MUST have WordPress. |
10
|
|
|
if ( ! defined( 'WPINC' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The main class responsible for registering CLI commands |
16
|
|
|
* @since 1.0.13 |
17
|
|
|
*/ |
18
|
|
|
class WPInv_CLI { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates a new invoice |
22
|
|
|
* |
23
|
|
|
* @param Array $args Arguments in array format. |
24
|
|
|
* @param Array $assoc_args Key value arguments stored in associated array format. |
25
|
|
|
* @since 1.0.13 |
26
|
|
|
*/ |
27
|
|
|
public function insert_invoice( $args, $assoc_args ) { |
|
|
|
|
28
|
|
|
|
29
|
|
|
// Fetch invoice data from the args |
30
|
|
|
$invoice_data = wp_unslash( $assoc_args ); |
31
|
|
|
|
32
|
|
|
// Abort if no invoice data is provided |
33
|
|
|
if ( empty( $invoice_data ) ) { |
34
|
|
|
return WP_CLI::error( __( 'Invoice data not provided', 'invoicing' ) ); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//Cart details |
38
|
|
|
if ( ! empty( $invoice_data['cart_details'] ) ) { |
39
|
|
|
$invoice_data['cart_details'] = json_decode( $invoice_data['cart_details'], true ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//User details |
43
|
|
|
if ( ! empty( $invoice_data['user_info'] ) ) { |
44
|
|
|
$invoice_data['user_info'] = json_decode( $invoice_data['user_info'], true ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
//Payment info |
48
|
|
|
if ( ! empty( $invoice_data['payment_details'] ) ) { |
49
|
|
|
$invoice_data['payment_details'] = json_decode( $invoice_data['payment_details'], true ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Try creating the invoice |
53
|
|
|
$invoice = wpinv_insert_invoice( $invoice_data, true ); |
54
|
|
|
|
55
|
|
|
if ( is_wp_error( $invoice ) ) { |
56
|
|
|
return WP_CLI::error( $invoice->get_error_message() ); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$message = sprintf( __( 'Invoice %s created', 'invoicing' ), $invoice->get_id() ); |
|
|
|
|
60
|
|
|
WP_CLI::success( $message ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.