PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.3 6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 All 156 releases
mainwp / modules / cost-tracker / rest-api / version1 / class-cost-tracker-rest-api-v1.php

class-cost-tracker-rest-api-v1.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at modules/cost-tracker/rest-api/version1/class-cost-tracker-rest-api-v1.php

345 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Module Cost Tracker Rest Api class.
4 *
5 * @package MainWP\Dashboard
6 * @version 4.6
7 */
8
9 namespace MainWP\Dashboard\Module\CostTracker;
10
11 use MainWP\Dashboard\MainWP_DB_Client;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Class Cost_Tracker_Rest_Api_V1
20 */
21 class Cost_Tracker_Rest_Api_V1 {
22
23 /**
24 * Protected variable to hold the API version.
25 *
26 * @var string API version
27 */
28 protected $api_version = '1';
29
30 /**
31 * Protected static variable to hold the single instance of the class.
32 *
33 * @var mixed Default null
34 */
35 private static $instance = null;
36
37 /**
38 * Method instance()
39 *
40 * Create public static instance.
41 *
42 * @static
43 * @return static::$instance
44 */
45 public static function instance() {
46 if ( null === static::$instance ) {
47 static::$instance = new self();
48 }
49 return static::$instance;
50 }
51
52 /**
53 * Method init()
54 *
55 * Adds an action to create the rest API endpoints if activated in the plugin settings.
56 */
57 public function init() {
58 add_action( 'init', array( &$this, 'init_rest_api' ) );
59 }
60
61 /**
62 * Method init_rest_api()
63 *
64 * Adds an action to create the rest API endpoints if activated in the plugin settings.
65 */
66 public function init_rest_api() {
67 $activated = apply_filters( 'mainwp_rest_api_enabled', false );
68 // only activate the api if enabled in the dashboard plugin.
69 if ( $activated ) {
70 // init APIs.
71 add_action( 'rest_api_init', array( &$this, 'mainwp_register_routes' ) );
72 }
73 }
74
75 /**
76 * Method mainwp_rest_api_init()
77 *
78 * Creates the necessary endpoints for the api.
79 * Note, for a request to be successful the URL query parameters consumer_key and consumer_secret need to be set and correct.
80 */
81 public function mainwp_register_routes() {
82 // Create an array which holds all the endpoints. Method can be GET, POST, PUT, DELETE.
83 $endpoints = array(
84 array(
85 'route' => 'cost-tracker',
86 'method' => 'GET',
87 'callback' => 'get-all-costs',
88 ),
89 array(
90 'route' => 'cost-tracker',
91 'method' => 'GET',
92 'callback' => 'get-client-costs',
93 ),
94 array(
95 'route' => 'cost-tracker',
96 'method' => 'GET',
97 'callback' => 'get-site-costs',
98 ),
99 array(
100 'route' => 'cost-tracker',
101 'method' => 'GET',
102 'callback' => 'get-costs',
103 ),
104 );
105
106 // loop through the endpoints.
107 foreach ( $endpoints as $endpoint ) {
108 $function_name = str_replace( '-', '_', $endpoint['callback'] );
109 register_rest_route(
110 'mainwp/v' . $this->api_version,
111 '/' . $endpoint['route'] . '/' . $endpoint['callback'],
112 array(
113 'methods' => $endpoint['method'],
114 'callback' => array( &$this, 'cost_tracker_rest_api_' . $function_name . '_callback' ),
115 'permission_callback' => '__return_true',
116 )
117 );
118 }
119 }
120
121 /**
122 * Method mainwp_authentication_error()
123 *
124 * Common error message when consumer key and secret are wrong.
125 *
126 * @return array $response Array with an error message explaining that the credentials are wrong.
127 */
128 public function mainwp_authentication_error() {
129
130 $data = array( 'ERROR' => esc_html__( 'Incorrect or missing consumer key and/or secret. If the issue persists please reset your authentication details from the MainWP > Settings > REST API page, on your MainWP Dashboard site.', 'mainwp' ) );
131
132 $response = new \WP_REST_Response( $data );
133 $response->set_status( 401 );
134
135 return $response;
136 }
137
138 /**
139 * Method mainwp_missing_data_error()
140 *
141 * Common error message when data is missing from the request.
142 *
143 * @return array $response Array with an error message explaining details are missing.
144 */
145 public function mainwp_missing_data_error() {
146
147 $data = array( 'ERROR' => esc_html__( 'Required parameter is missing.', 'mainwp' ) );
148
149 $response = new \WP_REST_Response( $data );
150 $response->set_status( 400 );
151
152 return $response;
153 }
154
155 /**
156 * Method mainwp_invalid_data_error()
157 *
158 * Common error message when data in request is ivalid.
159 *
160 * @return array $response Array with an error message explaining details are missing.
161 */
162 public function mainwp_invalid_data_error() {
163
164 $data = array( 'ERROR' => esc_html__( 'Required parameter data is is not valid.', 'mainwp' ) );
165
166 $response = new \WP_REST_Response( $data );
167 $response->set_status( 400 );
168
169 return $response;
170 }
171
172 /**
173 * Method cost_tracker_rest_api_get_all_costs_callback()
174 *
175 * Callback function for managing the response to API requests made for the endpoint: cost-tracker
176 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/cost-tracker/get-all-costs
177 * API Method: GET
178 *
179 * @param array $request The request made in the API call which includes all parameters.
180 *
181 * @return object $response An object that contains the return data and status of the API request.
182 */
183 public function cost_tracker_rest_api_get_all_costs_callback( $request ) {
184 // first validate the request.
185 if ( apply_filters( 'mainwp_rest_api_validate', false, $request ) ) {
186 $costs = Cost_Tracker_DB::get_instance()->get_cost_tracker_by( 'all' );
187 $result = array();
188 $data = Cost_Tracker_Rest_Api_Handle_V1::instance()->prepare_api_costs_data( $costs );
189 $result['data'] = $data;
190 $response = new \WP_REST_Response( $result );
191 $response->set_status( 200 );
192 } else {
193 // throw common error.
194 $response = $this->mainwp_authentication_error();
195 }
196 return $response;
197 }
198
199
200 /**
201 * Method cost_tracker_rest_api_get_client_costs_callback()
202 *
203 * Callback function for managing the response to API requests made for the endpoint: cost-tracker
204 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/cost-tracker/get-client-costs
205 * API Method: GET
206 *
207 * @param array $request The request made in the API call which includes all parameters.
208 *
209 * @return object $response An object that contains the return data and status of the API request.
210 */
211 public function cost_tracker_rest_api_get_client_costs_callback( $request ) { //phpcs:ignore -- NOSONAR - complex.
212 // first validate the request.
213 if ( apply_filters( 'mainwp_rest_api_validate', false, $request ) ) {
214 if ( isset( $request['client_id'] ) && null !== $request['client_id'] ) {
215 $client_id = intval( $request['client_id'] );
216 $costs = array();
217 $error = '';
218 $client = false;
219 if ( ! empty( $client_id ) ) {
220 $client = MainWP_DB_Client::instance()->get_wp_client_by( 'client_id', $client_id );
221 }
222
223 if ( empty( $client ) ) {
224 $error = esc_html__( 'Invalid Client ID or Client not found. Please try again.', 'mainwp' );
225 } else {
226 $costs = Cost_Tracker_DB::get_instance()->get_cost_tracker_by( 'client_id', $client_id );
227 $data = Cost_Tracker_Rest_Api_Handle_V1::instance()->prepare_api_costs_data( $costs );
228
229 $result = array();
230 $result['data'] = $data;
231 }
232 if ( ! empty( $error ) ) {
233 $result['ERROR'] = $error;
234 }
235
236 $response = new \WP_REST_Response( $result );
237 $response->set_status( 200 );
238 } else {
239 // throw missing data error.
240 $response = $this->mainwp_missing_data_error();
241 }
242 } else {
243 // throw common error.
244 $response = $this->mainwp_authentication_error();
245 }
246 return $response;
247 }
248
249
250 /**
251 * Method cost_tracker_rest_api_get_site_costs_callback()
252 *
253 * Callback function for managing the response to API requests made for the endpoint: cost-tracker
254 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/cost-tracker/get-site-costs
255 * API Method: GET
256 *
257 * @param array $request The request made in the API call which includes all parameters.
258 *
259 * @return object $response An object that contains the return data and status of the API request.
260 */
261 public function cost_tracker_rest_api_get_site_costs_callback( $request ) {
262 // first validate the request.
263 if ( apply_filters( 'mainwp_rest_api_validate', false, $request ) ) {
264 // get parameters.
265 if ( isset( $request['site_id'] ) && null !== $request['site_id'] ) {
266 $site_id = intval( $request['site_id'] );
267 $result = array();
268
269 $valid_error = Cost_Tracker_Rest_Api_Handle_V1::instance()->valid_api_request_data_by( 'site_id', $site_id );
270 if ( ! empty( $valid_error ) ) {
271 $result = $valid_error;
272 } else {
273 $costs = Cost_Tracker_DB::get_instance()->get_cost_tracker_by( 'site_id', $site_id );
274 $data = Cost_Tracker_Rest_Api_Handle_V1::instance()->prepare_api_costs_data( $costs );
275 $result['data'] = $data;
276 }
277 $response = new \WP_REST_Response( $result );
278 $response->set_status( 200 );
279 } else {
280 // throw missing data error.
281 $response = $this->mainwp_missing_data_error();
282 }
283 } else {
284 // throw common error.
285 $response = $this->mainwp_authentication_error();
286 }
287 return $response;
288 }
289
290 /**
291 * Method cost_tracker_rest_api_get_costs_callback()
292 *
293 * Callback function for managing the response to API requests made for the endpoint: cost-tracker
294 * Can be accessed via a request like: https://yourdomain.com/wp-json/mainwp/v1/cost-tracker/get-costs
295 * API Method: GET
296 *
297 * @param array $request The request made in the API call which includes all parameters.
298 *
299 * @return object $response An object that contains the return data and status of the API request.
300 */
301 public function cost_tracker_rest_api_get_costs_callback( $request ) { //phpcs:ignore -- NOSONAR - complex.
302 // first validate the request.
303 if ( apply_filters( 'mainwp_rest_api_validate', false, $request ) ) {
304 // get parameters.
305 if ( isset( $request['id'] ) && null !== $request['id'] ) {
306 $id = sanitize_text_field( $request['id'] ); // int or string of int.
307 $costs_data = array();
308 if ( ! empty( $id ) ) {
309 $cost = Cost_Tracker_DB::get_instance()->get_cost_tracker_by( 'id', $request['id'] );
310 if ( is_numeric( $id ) ) {
311 $costs_data[] = $cost;
312 } else {
313 $costs_data = $cost;
314 }
315 }
316
317 $result = array();
318 $error = '';
319 if ( empty( $cost ) ) {
320 $error = esc_html__( 'Invaid subscription id or subscription not found. Plase try again.', 'mainwp' );
321 } else {
322 $data = Cost_Tracker_Rest_Api_Handle_V1::instance()->prepare_api_costs_data( $costs_data );
323 $result['data'] = $data;
324 }
325
326 if ( ! empty( $error ) ) {
327 $result['ERROR'] = $error;
328 }
329
330 $response = new \WP_REST_Response( $result );
331 $response->set_status( 200 );
332 } else {
333 // throw missing data error.
334 $response = $this->mainwp_missing_data_error();
335 }
336 } else {
337 // throw common error.
338 $response = $this->mainwp_authentication_error();
339 }
340 return $response;
341 }
342 }
343
344 // End of class.
345