|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST invoices controllers. |
|
4
|
|
|
* |
|
5
|
|
|
* @version 1.0.19 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
defined( 'ABSPATH' ) || exit; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* REST API invoices controller class. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Invoicing |
|
14
|
|
|
*/ |
|
15
|
|
|
class WPInv_REST_Invoice_Controller extends GetPaid_REST_Posts_Controller { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Post type. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $post_type = 'wpi_invoice'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The base of this controller's route. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0.13 |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $rest_base = 'invoices'; |
|
31
|
|
|
|
|
32
|
|
|
/** Contains this controller's class name. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public $crud_class = 'WPInv_Invoice'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Retrieves the query params for the invoices collection. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 1.0.13 |
|
42
|
|
|
* |
|
43
|
|
|
* @return array Collection parameters. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get_collection_params() { |
|
46
|
|
|
|
|
47
|
|
|
$params = array_merge( |
|
48
|
|
|
|
|
49
|
|
|
parent::get_collection_params(), |
|
50
|
|
|
|
|
51
|
|
|
array( |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
'customers' => array( |
|
55
|
|
|
'description' => __( 'Limit result set to invoices for specific user ids.', 'invoicing' ), |
|
56
|
|
|
'type' => 'array', |
|
57
|
|
|
'items' => array( |
|
58
|
|
|
'type' => 'integer', |
|
59
|
|
|
), |
|
60
|
|
|
'default' => array(), |
|
61
|
|
|
'sanitize_callback' => 'wp_parse_id_list', |
|
62
|
|
|
), |
|
63
|
|
|
|
|
64
|
|
|
'exclude_customers' => array( |
|
65
|
|
|
'description' => __( 'Exclude invoices to specific users.', 'invoicing' ), |
|
66
|
|
|
'type' => 'array', |
|
67
|
|
|
'items' => array( |
|
68
|
|
|
'type' => 'integer', |
|
69
|
|
|
), |
|
70
|
|
|
'default' => array(), |
|
71
|
|
|
'sanitize_callback' => 'wp_parse_id_list', |
|
72
|
|
|
) |
|
73
|
|
|
|
|
74
|
|
|
) |
|
75
|
|
|
|
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
// Filter collection parameters for the invoices controller. |
|
79
|
|
|
return apply_filters( 'getpaid_rest_invoices_collection_params', $params, $this ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get all the WP Query vars that are allowed for the API request. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function get_allowed_query_vars() { |
|
88
|
|
|
|
|
89
|
|
|
$vars = array_merge( |
|
90
|
|
|
array( |
|
91
|
|
|
'customers', |
|
92
|
|
|
'exclude_customers' |
|
93
|
|
|
), |
|
94
|
|
|
parent::get_allowed_query_vars() |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
return apply_filters( 'getpaid_rest_invoices_allowed_query_vars', $vars, $this ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Determine the allowed query_vars for a get_items() response and |
|
102
|
|
|
* prepare for WP_Query. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $prepared_args Prepared arguments. |
|
105
|
|
|
* @param WP_REST_Request $request Request object. |
|
106
|
|
|
* @return array $query_args |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
109
|
|
|
|
|
110
|
|
|
$query_args = parent::prepare_items_query( $prepared_args ); |
|
111
|
|
|
|
|
112
|
|
|
// Retrieve invoices for specific customers. |
|
113
|
|
|
if ( isset( $query_args['customers'] ) ) { |
|
114
|
|
|
$query_args['author__in'] = $query_args['customers']; |
|
115
|
|
|
unset( $query_args['customers'] ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Skip invoices for specific customers. |
|
119
|
|
|
if ( isset( $query_args['exclude_customers'] ) ) { |
|
120
|
|
|
$query_args['author__not_in'] = $query_args['exclude_customers']; |
|
121
|
|
|
unset( $query_args['exclude_customers'] ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return apply_filters( 'getpaid_rest_invoices_prepare_items_query', $query_args, $request, $this ); |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Retrieves a valid list of post statuses. |
|
130
|
|
|
* |
|
131
|
|
|
* @since 1.0.15 |
|
132
|
|
|
* |
|
133
|
|
|
* @return array A list of registered item statuses. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function get_post_statuses() { |
|
136
|
|
|
return array_keys( wpinv_get_invoice_statuses( true ) ); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Saves a single invoice. |
|
141
|
|
|
* |
|
142
|
|
|
* @param WPInv_Invoice $invoice Invoice to save. |
|
143
|
|
|
* @return WP_Error|WPInv_Invoice |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function save_object( $invoice ) { |
|
146
|
|
|
$invoice->recalculate_total(); |
|
147
|
|
|
return parent::save_object( $invoice ); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|