@@ -18,102 +18,102 @@ |
||
18 | 18 | */ |
19 | 19 | class GetPaid_REST_Report_Invoice_Counts_Controller extends GetPaid_REST_Reports_Controller { |
20 | 20 | |
21 | - /** |
|
22 | - * Route base. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $rest_base = 'reports/invoices/counts'; |
|
27 | - |
|
28 | - /** |
|
29 | - * Prepare a report object for serialization. |
|
30 | - * |
|
31 | - * @param stdClass $report Report data. |
|
32 | - * @param WP_REST_Request $request Request object. |
|
33 | - * @return WP_REST_Response $response Response data. |
|
34 | - */ |
|
35 | - public function prepare_item_for_response( $report, $request ) { |
|
36 | - |
|
37 | - $data = (array) $report; |
|
38 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
39 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
40 | - $data = $this->filter_response_by_context( $data, $context ); |
|
41 | - |
|
42 | - // Wrap the data in a response object. |
|
43 | - $response = rest_ensure_response( $data ); |
|
44 | - |
|
45 | - $response->add_links( |
|
46 | - array( |
|
47 | - 'about' => array( |
|
48 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
49 | - ), |
|
50 | - ) |
|
51 | - ); |
|
52 | - |
|
53 | - return apply_filters( 'getpaid_rest_prepare_report_invoices_count', $response, $report, $request ); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Get reports list. |
|
58 | - * |
|
59 | - * @since 2.0.0 |
|
60 | - * @return array |
|
61 | - */ |
|
62 | - protected function get_reports() { |
|
63 | - |
|
64 | - $counts = wp_count_posts( 'wpi_invoice' ); |
|
65 | - $data = array(); |
|
66 | - |
|
67 | - foreach ( wpinv_get_invoice_statuses() as $slug => $name ) { |
|
68 | - |
|
69 | - if ( ! isset( $counts->$slug ) ) { |
|
70 | - continue; |
|
71 | - } |
|
72 | - |
|
73 | - $data[] = array( |
|
74 | - 'slug' => $slug, |
|
75 | - 'name' => $name, |
|
76 | - 'count' => (int) $counts->$slug, |
|
77 | - ); |
|
78 | - |
|
79 | - } |
|
80 | - |
|
81 | - return $data; |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get the Report's schema, conforming to JSON Schema. |
|
87 | - * |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - public function get_item_schema() { |
|
91 | - $schema = array( |
|
92 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
93 | - 'title' => 'report_invoice_counts', |
|
94 | - 'type' => 'object', |
|
95 | - 'properties' => array( |
|
96 | - 'slug' => array( |
|
97 | - 'description' => __( 'An alphanumeric identifier for the resource.', 'invoicing' ), |
|
98 | - 'type' => 'string', |
|
99 | - 'context' => array( 'view' ), |
|
100 | - 'readonly' => true, |
|
101 | - ), |
|
102 | - 'name' => array( |
|
103 | - 'description' => __( 'Invoice status name.', 'invoicing' ), |
|
104 | - 'type' => 'string', |
|
105 | - 'context' => array( 'view' ), |
|
106 | - 'readonly' => true, |
|
107 | - ), |
|
108 | - 'count' => array( |
|
109 | - 'description' => __( 'Number of invoices.', 'invoicing' ), |
|
110 | - 'type' => 'string', |
|
111 | - 'context' => array( 'view' ), |
|
112 | - 'readonly' => true, |
|
113 | - ), |
|
114 | - ), |
|
115 | - ); |
|
116 | - |
|
117 | - return $this->add_additional_fields_schema( $schema ); |
|
118 | - } |
|
21 | + /** |
|
22 | + * Route base. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $rest_base = 'reports/invoices/counts'; |
|
27 | + |
|
28 | + /** |
|
29 | + * Prepare a report object for serialization. |
|
30 | + * |
|
31 | + * @param stdClass $report Report data. |
|
32 | + * @param WP_REST_Request $request Request object. |
|
33 | + * @return WP_REST_Response $response Response data. |
|
34 | + */ |
|
35 | + public function prepare_item_for_response( $report, $request ) { |
|
36 | + |
|
37 | + $data = (array) $report; |
|
38 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
39 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
40 | + $data = $this->filter_response_by_context( $data, $context ); |
|
41 | + |
|
42 | + // Wrap the data in a response object. |
|
43 | + $response = rest_ensure_response( $data ); |
|
44 | + |
|
45 | + $response->add_links( |
|
46 | + array( |
|
47 | + 'about' => array( |
|
48 | + 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
49 | + ), |
|
50 | + ) |
|
51 | + ); |
|
52 | + |
|
53 | + return apply_filters( 'getpaid_rest_prepare_report_invoices_count', $response, $report, $request ); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Get reports list. |
|
58 | + * |
|
59 | + * @since 2.0.0 |
|
60 | + * @return array |
|
61 | + */ |
|
62 | + protected function get_reports() { |
|
63 | + |
|
64 | + $counts = wp_count_posts( 'wpi_invoice' ); |
|
65 | + $data = array(); |
|
66 | + |
|
67 | + foreach ( wpinv_get_invoice_statuses() as $slug => $name ) { |
|
68 | + |
|
69 | + if ( ! isset( $counts->$slug ) ) { |
|
70 | + continue; |
|
71 | + } |
|
72 | + |
|
73 | + $data[] = array( |
|
74 | + 'slug' => $slug, |
|
75 | + 'name' => $name, |
|
76 | + 'count' => (int) $counts->$slug, |
|
77 | + ); |
|
78 | + |
|
79 | + } |
|
80 | + |
|
81 | + return $data; |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get the Report's schema, conforming to JSON Schema. |
|
87 | + * |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + public function get_item_schema() { |
|
91 | + $schema = array( |
|
92 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
93 | + 'title' => 'report_invoice_counts', |
|
94 | + 'type' => 'object', |
|
95 | + 'properties' => array( |
|
96 | + 'slug' => array( |
|
97 | + 'description' => __( 'An alphanumeric identifier for the resource.', 'invoicing' ), |
|
98 | + 'type' => 'string', |
|
99 | + 'context' => array( 'view' ), |
|
100 | + 'readonly' => true, |
|
101 | + ), |
|
102 | + 'name' => array( |
|
103 | + 'description' => __( 'Invoice status name.', 'invoicing' ), |
|
104 | + 'type' => 'string', |
|
105 | + 'context' => array( 'view' ), |
|
106 | + 'readonly' => true, |
|
107 | + ), |
|
108 | + 'count' => array( |
|
109 | + 'description' => __( 'Number of invoices.', 'invoicing' ), |
|
110 | + 'type' => 'string', |
|
111 | + 'context' => array( 'view' ), |
|
112 | + 'readonly' => true, |
|
113 | + ), |
|
114 | + ), |
|
115 | + ); |
|
116 | + |
|
117 | + return $this->add_additional_fields_schema( $schema ); |
|
118 | + } |
|
119 | 119 | } |
@@ -18,188 +18,188 @@ |
||
18 | 18 | */ |
19 | 19 | class GetPaid_REST_Reports_Controller extends GetPaid_REST_Controller { |
20 | 20 | |
21 | - /** |
|
22 | - * Route base. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $rest_base = 'reports'; |
|
27 | - |
|
28 | - /** |
|
29 | - * Registers the routes for the objects of the controller. |
|
30 | - * |
|
31 | - * @since 2.0.0 |
|
32 | - * |
|
33 | - * @see register_rest_route() |
|
34 | - */ |
|
35 | - public function register_namespace_routes( $namespace ) { |
|
36 | - |
|
37 | - // List all available reports. |
|
38 | - register_rest_route( |
|
39 | - $namespace, |
|
40 | - $this->rest_base, |
|
41 | - array( |
|
42 | - array( |
|
43 | - 'methods' => WP_REST_Server::READABLE, |
|
44 | - 'callback' => array( $this, 'get_items' ), |
|
45 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
46 | - 'args' => $this->get_collection_params(), |
|
47 | - ), |
|
48 | - 'schema' => array( $this, 'get_public_item_schema' ), |
|
49 | - ) |
|
50 | - ); |
|
51 | - |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Makes sure the current user has access to READ the report APIs. |
|
56 | - * |
|
57 | - * @since 2.0.0 |
|
58 | - * @param WP_REST_Request $request Full data about the request. |
|
59 | - * @return WP_Error|boolean |
|
60 | - */ |
|
61 | - public function get_items_permissions_check( $request ) { |
|
62 | - |
|
63 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
64 | - return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
65 | - } |
|
66 | - |
|
67 | - return true; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Get reports list. |
|
72 | - * |
|
73 | - * @since 2.0.0 |
|
74 | - * @return array |
|
75 | - */ |
|
76 | - protected function get_reports() { |
|
77 | - |
|
78 | - $reports = array( |
|
79 | - array( |
|
80 | - 'slug' => 'sales', |
|
81 | - 'description' => __( 'List of sales reports.', 'invoicing' ), |
|
82 | - ), |
|
83 | - array( |
|
84 | - 'slug' => 'top_sellers', |
|
85 | - 'description' => __( 'List of top selling items.', 'invoicing' ), |
|
86 | - ), |
|
87 | - array( |
|
88 | - 'slug' => 'top_earners', |
|
89 | - 'description' => __( 'List of top earning items.', 'invoicing' ), |
|
90 | - ), |
|
91 | - array( |
|
92 | - 'slug' => 'invoices/counts', |
|
93 | - 'description' => __( 'Invoice counts.', 'invoicing' ), |
|
94 | - ), |
|
95 | - array( |
|
96 | - 'slug' => 'items/totals', |
|
97 | - 'description' => __( 'Item totals.', 'invoicing' ), |
|
98 | - ), |
|
99 | - array( |
|
100 | - 'slug' => 'customers/totals', |
|
101 | - 'description' => __( 'Customer totals.', 'invoicing' ), |
|
102 | - ), |
|
103 | - array( |
|
104 | - 'slug' => 'discounts/totals', |
|
105 | - 'description' => __( 'Discount totals.', 'invoicing' ), |
|
106 | - ) |
|
107 | - ); |
|
108 | - |
|
109 | - return apply_filters( 'getpaid_available_api_reports', $reports ); |
|
110 | - |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get all reports. |
|
115 | - * |
|
116 | - * @since 2.0.0 |
|
117 | - * @param WP_REST_Request $request |
|
118 | - * @return array|WP_Error |
|
119 | - */ |
|
120 | - public function get_items( $request ) { |
|
121 | - $data = array(); |
|
122 | - $reports = $this->get_reports(); |
|
123 | - |
|
124 | - foreach ( $reports as $report ) { |
|
125 | - $item = $this->prepare_item_for_response( (object) $report, $request ); |
|
126 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
127 | - } |
|
128 | - |
|
129 | - return rest_ensure_response( $data ); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Prepare a report object for serialization. |
|
134 | - * |
|
135 | - * @since 2.0.0 |
|
136 | - * @param stdClass $report Report data. |
|
137 | - * @param WP_REST_Request $request Request object. |
|
138 | - * @return WP_REST_Response $response Response data. |
|
139 | - */ |
|
140 | - public function prepare_item_for_response( $report, $request ) { |
|
141 | - $data = array( |
|
142 | - 'slug' => $report->slug, |
|
143 | - 'description' => $report->description, |
|
144 | - ); |
|
145 | - |
|
146 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
147 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
148 | - $data = $this->filter_response_by_context( $data, $context ); |
|
149 | - |
|
150 | - // Wrap the data in a response object. |
|
151 | - $response = rest_ensure_response( $data ); |
|
152 | - $response->add_links( array( |
|
153 | - 'self' => array( |
|
154 | - 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ), |
|
155 | - ), |
|
156 | - 'collection' => array( |
|
157 | - 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
|
158 | - ), |
|
159 | - ) ); |
|
160 | - |
|
161 | - return apply_filters( 'getpaid_rest_prepare_report', $response, $report, $request ); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Get the Report's schema, conforming to JSON Schema. |
|
166 | - * |
|
167 | - * @since 2.0.0 |
|
168 | - * @return array |
|
169 | - */ |
|
170 | - public function get_item_schema() { |
|
171 | - $schema = array( |
|
172 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
173 | - 'title' => 'report', |
|
174 | - 'type' => 'object', |
|
175 | - 'properties' => array( |
|
176 | - 'slug' => array( |
|
177 | - 'description' => __( 'An alphanumeric identifier for the resource.', 'invoicing' ), |
|
178 | - 'type' => 'string', |
|
179 | - 'context' => array( 'view' ), |
|
180 | - 'readonly' => true, |
|
181 | - ), |
|
182 | - 'description' => array( |
|
183 | - 'description' => __( 'A human-readable description of the resource.', 'invoicing' ), |
|
184 | - 'type' => 'string', |
|
185 | - 'context' => array( 'view' ), |
|
186 | - 'readonly' => true, |
|
187 | - ), |
|
188 | - ), |
|
189 | - ); |
|
190 | - |
|
191 | - return $this->add_additional_fields_schema( $schema ); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Get the query params for collections. |
|
196 | - * |
|
197 | - * @since 2.0.0 |
|
198 | - * @return array |
|
199 | - */ |
|
200 | - public function get_collection_params() { |
|
201 | - return array( |
|
202 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
203 | - ); |
|
204 | - } |
|
21 | + /** |
|
22 | + * Route base. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $rest_base = 'reports'; |
|
27 | + |
|
28 | + /** |
|
29 | + * Registers the routes for the objects of the controller. |
|
30 | + * |
|
31 | + * @since 2.0.0 |
|
32 | + * |
|
33 | + * @see register_rest_route() |
|
34 | + */ |
|
35 | + public function register_namespace_routes( $namespace ) { |
|
36 | + |
|
37 | + // List all available reports. |
|
38 | + register_rest_route( |
|
39 | + $namespace, |
|
40 | + $this->rest_base, |
|
41 | + array( |
|
42 | + array( |
|
43 | + 'methods' => WP_REST_Server::READABLE, |
|
44 | + 'callback' => array( $this, 'get_items' ), |
|
45 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
46 | + 'args' => $this->get_collection_params(), |
|
47 | + ), |
|
48 | + 'schema' => array( $this, 'get_public_item_schema' ), |
|
49 | + ) |
|
50 | + ); |
|
51 | + |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Makes sure the current user has access to READ the report APIs. |
|
56 | + * |
|
57 | + * @since 2.0.0 |
|
58 | + * @param WP_REST_Request $request Full data about the request. |
|
59 | + * @return WP_Error|boolean |
|
60 | + */ |
|
61 | + public function get_items_permissions_check( $request ) { |
|
62 | + |
|
63 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
64 | + return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
65 | + } |
|
66 | + |
|
67 | + return true; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Get reports list. |
|
72 | + * |
|
73 | + * @since 2.0.0 |
|
74 | + * @return array |
|
75 | + */ |
|
76 | + protected function get_reports() { |
|
77 | + |
|
78 | + $reports = array( |
|
79 | + array( |
|
80 | + 'slug' => 'sales', |
|
81 | + 'description' => __( 'List of sales reports.', 'invoicing' ), |
|
82 | + ), |
|
83 | + array( |
|
84 | + 'slug' => 'top_sellers', |
|
85 | + 'description' => __( 'List of top selling items.', 'invoicing' ), |
|
86 | + ), |
|
87 | + array( |
|
88 | + 'slug' => 'top_earners', |
|
89 | + 'description' => __( 'List of top earning items.', 'invoicing' ), |
|
90 | + ), |
|
91 | + array( |
|
92 | + 'slug' => 'invoices/counts', |
|
93 | + 'description' => __( 'Invoice counts.', 'invoicing' ), |
|
94 | + ), |
|
95 | + array( |
|
96 | + 'slug' => 'items/totals', |
|
97 | + 'description' => __( 'Item totals.', 'invoicing' ), |
|
98 | + ), |
|
99 | + array( |
|
100 | + 'slug' => 'customers/totals', |
|
101 | + 'description' => __( 'Customer totals.', 'invoicing' ), |
|
102 | + ), |
|
103 | + array( |
|
104 | + 'slug' => 'discounts/totals', |
|
105 | + 'description' => __( 'Discount totals.', 'invoicing' ), |
|
106 | + ) |
|
107 | + ); |
|
108 | + |
|
109 | + return apply_filters( 'getpaid_available_api_reports', $reports ); |
|
110 | + |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get all reports. |
|
115 | + * |
|
116 | + * @since 2.0.0 |
|
117 | + * @param WP_REST_Request $request |
|
118 | + * @return array|WP_Error |
|
119 | + */ |
|
120 | + public function get_items( $request ) { |
|
121 | + $data = array(); |
|
122 | + $reports = $this->get_reports(); |
|
123 | + |
|
124 | + foreach ( $reports as $report ) { |
|
125 | + $item = $this->prepare_item_for_response( (object) $report, $request ); |
|
126 | + $data[] = $this->prepare_response_for_collection( $item ); |
|
127 | + } |
|
128 | + |
|
129 | + return rest_ensure_response( $data ); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Prepare a report object for serialization. |
|
134 | + * |
|
135 | + * @since 2.0.0 |
|
136 | + * @param stdClass $report Report data. |
|
137 | + * @param WP_REST_Request $request Request object. |
|
138 | + * @return WP_REST_Response $response Response data. |
|
139 | + */ |
|
140 | + public function prepare_item_for_response( $report, $request ) { |
|
141 | + $data = array( |
|
142 | + 'slug' => $report->slug, |
|
143 | + 'description' => $report->description, |
|
144 | + ); |
|
145 | + |
|
146 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
147 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
148 | + $data = $this->filter_response_by_context( $data, $context ); |
|
149 | + |
|
150 | + // Wrap the data in a response object. |
|
151 | + $response = rest_ensure_response( $data ); |
|
152 | + $response->add_links( array( |
|
153 | + 'self' => array( |
|
154 | + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug ) ), |
|
155 | + ), |
|
156 | + 'collection' => array( |
|
157 | + 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
|
158 | + ), |
|
159 | + ) ); |
|
160 | + |
|
161 | + return apply_filters( 'getpaid_rest_prepare_report', $response, $report, $request ); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Get the Report's schema, conforming to JSON Schema. |
|
166 | + * |
|
167 | + * @since 2.0.0 |
|
168 | + * @return array |
|
169 | + */ |
|
170 | + public function get_item_schema() { |
|
171 | + $schema = array( |
|
172 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
173 | + 'title' => 'report', |
|
174 | + 'type' => 'object', |
|
175 | + 'properties' => array( |
|
176 | + 'slug' => array( |
|
177 | + 'description' => __( 'An alphanumeric identifier for the resource.', 'invoicing' ), |
|
178 | + 'type' => 'string', |
|
179 | + 'context' => array( 'view' ), |
|
180 | + 'readonly' => true, |
|
181 | + ), |
|
182 | + 'description' => array( |
|
183 | + 'description' => __( 'A human-readable description of the resource.', 'invoicing' ), |
|
184 | + 'type' => 'string', |
|
185 | + 'context' => array( 'view' ), |
|
186 | + 'readonly' => true, |
|
187 | + ), |
|
188 | + ), |
|
189 | + ); |
|
190 | + |
|
191 | + return $this->add_additional_fields_schema( $schema ); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Get the query params for collections. |
|
196 | + * |
|
197 | + * @since 2.0.0 |
|
198 | + * @return array |
|
199 | + */ |
|
200 | + public function get_collection_params() { |
|
201 | + return array( |
|
202 | + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
203 | + ); |
|
204 | + } |
|
205 | 205 | } |