Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor/ecommerce/BillingController.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Manage Billing
4
+
*
5
+
* @package Tutor\Ecommerce
6
+
* @author Themeum
7
+
* @link https://themeum.com
8
+
* @since 3.0.0
9
+
*/
10
+
11
+
namespace Tutor\Ecommerce;
12
+
13
+
use TUTOR\BaseController;
14
+
use Tutor\Helpers\HttpHelper;
15
+
use Tutor\Helpers\ValidationHelper;
16
+
use Tutor\Models\BillingModel;
17
+
use Tutor\Traits\JsonResponse;
18
+
19
+
if ( ! defined( 'ABSPATH' ) ) {
20
+
exit;
21
+
}
22
+
23
+
/**
24
+
* BillingController class
25
+
*
26
+
* @since 3.0.0
27
+
*/
28
+
class BillingController extends BaseController {
29
+
30
+
31
+
/**
32
+
* Billing model
33
+
*
34
+
* @since 3.0.0
35
+
*
36
+
* @var BillingModel
37
+
*/
38
+
private $model;
39
+
40
+
/**
41
+
* Trait for sending JSON response
42
+
*/
43
+
use JsonResponse;
44
+
45
+
/**
46
+
* Constructor.
47
+
*
48
+
* Initializes the Billing class, sets the page title, and optionally registers
49
+
* hooks for handling AJAX requests related to billing data.
50
+
*
51
+
* @param bool $register_hooks Whether to register hooks for handling requests. Default is true.
52
+
*
53
+
* @since 3.0.0
54
+
*
55
+
* @return void
56
+
*/
57
+
public function __construct( $register_hooks = true ) {
58
+
$this->model = new BillingModel();
59
+
60
+
if ( $register_hooks ) {
61
+
add_filter( 'tutor_dashboard/nav_items/settings/nav_items', array( $this, 'register_nav' ) );
62
+
add_filter( 'load_dashboard_template_part_from_other_location', array( $this, 'load_template' ) );
63
+
64
+
/**
65
+
* Handle AJAX request for saving billing info if current user.
66
+
*
67
+
* @since 3.0.0
68
+
*/
69
+
add_action( 'wp_ajax_tutor_save_billing_info', array( $this, 'save_billing_info' ) );
70
+
71
+
/**
72
+
* Handle AJAX request for getting billing info if current user.
73
+
*
74
+
* @since 3.0.0
75
+
*/
76
+
add_action( 'wp_ajax_tutor_get_billing_info', array( $this, 'get_billing_info' ) );
77
+
}
78
+
}
79
+
80
+
/**
81
+
* Register billing nav menu for settings
82
+
*
83
+
* @since 3.0.0
84
+
*
85
+
* @param array $tabs setting navigation tabs.
86
+
*
87
+
* @return array
88
+
*/
89
+
public static function register_nav( $tabs ) {
90
+
$billing_url = tutor_utils()->get_tutor_dashboard_page_permalink( 'settings/billing' );
91
+
92
+
$new_tab = array(
93
+
'url' => esc_url( $billing_url ),
94
+
'title' => __( 'Billing', 'tutor' ),
95
+
'role' => false,
96
+
);
97
+
98
+
$tabs['billing'] = $new_tab;
99
+
100
+
return $tabs;
101
+
}
102
+
103
+
/**
104
+
* Load billing template for settings
105
+
*
106
+
* Based on query_vars filter template path
107
+
*
108
+
* @since 3.0.0
109
+
*
110
+
* @param string $location default file location.
111
+
*
112
+
* @return string
113
+
*/
114
+
public static function load_template( $location ) {
115
+
$page_name = get_query_var( 'pagename' );
116
+
$dashboard_sub_page = get_query_var( 'tutor_dashboard_sub_page' );
117
+
118
+
$dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
119
+
$dashboard_page = get_post( $dashboard_page_id );
120
+
121
+
// Current page is dashboard & sub page is billing.
122
+
if ( $page_name === $dashboard_page->post_name && 'billing' === $dashboard_sub_page ) {
123
+
$template = tutor()->path . 'templates/ecommerce/billing.php';
124
+
125
+
if ( file_exists( $template ) ) {
126
+
$location = $template;
127
+
}
128
+
}
129
+
130
+
return $location;
131
+
}
132
+
133
+
/**
134
+
* Get the customer model object
135
+
*
136
+
* @since 3.0.0
137
+
*
138
+
* @return object
139
+
*/
140
+
public function get_model() {
141
+
return $this->model;
142
+
}
143
+
144
+
/**
145
+
* Save billing information for the current user.
146
+
*
147
+
* @since 3.0.0
148
+
*
149
+
* @return void
150
+
*/
151
+
public function save_billing_info() {
152
+
if ( ! tutor_utils()->is_nonce_verified() ) {
153
+
$this->json_response(
154
+
tutor_utils()->error_message( 'nonce' ),
155
+
null,
156
+
HttpHelper::STATUS_BAD_REQUEST
157
+
);
158
+
}
159
+
160
+
$data = $this->get_allowed_fields( $_POST );
161
+
162
+
$user_id = get_current_user_id();
163
+
$data['user_id'] = $user_id;
164
+
165
+
$validation = $this->validate( $data );
166
+
if ( ! $validation->success ) {
167
+
$this->json_response(
168
+
__( 'Invalid inputs', 'tutor' ),
169
+
$validation->errors,
170
+
HttpHelper::STATUS_UNPROCESSABLE_ENTITY
171
+
);
172
+
}
173
+
174
+
$billing_info = $this->get_billing_info();
175
+
176
+
if ( $billing_info ) {
177
+
$response = $this->model->update( $data, array( 'user_id' => $user_id ) );
178
+
} else {
179
+
$response = $this->model->insert( $data );
180
+
}
181
+
182
+
if ( ! $response ) {
183
+
$this->json_response(
184
+
__( 'Failed to save billing info', 'tutor' ),
185
+
null,
186
+
HttpHelper::STATUS_INTERNAL_SERVER_ERROR
187
+
);
188
+
}
189
+
190
+
$this->json_response( __( 'Billing info saved successfully', 'tutor' ) );
191
+
}
192
+
193
+
/**
194
+
* Get billing information for the current user.
195
+
*
196
+
* @since 3.0.0
197
+
*
198
+
* @param int $user_id User id.
199
+
*
200
+
* @return mixed The user's billing information.
201
+
*/
202
+
public function get_billing_info( $user_id = 0 ) {
203
+
$user_id = tutor_utils()->get_user_id( $user_id );
204
+
return $this->model->get_info( $user_id );
205
+
}
206
+
207
+
/**
208
+
* Validate input data based on predefined rules.
209
+
*
210
+
* This protected method validates the provided data array against a set of
211
+
* predefined validation rules. The rules specify that 'order_id' is required
212
+
* and must be numeric. The method will skip validation rules for fields that
213
+
* are not present in the data array.
214
+
*
215
+
* @since 3.0.0
216
+
*
217
+
* @param array $data The data array to validate.
218
+
*
219
+
* @return object The validation result. It returns validation object.
220
+
*/
221
+
protected function validate( array $data ) {
222
+
223
+
$validation_rules = array(
224
+
'user_id' => 'required|numeric',
225
+
'first_name' => 'required',
226
+
'last_name' => 'required',
227
+
'email' => 'required|email',
228
+
'phone' => 'required',
229
+
'zip_code' => 'required',
230
+
'address' => 'required',
231
+
'country' => 'required',
232
+
'state' => 'required',
233
+
'city' => 'required',
234
+
);
235
+
236
+
// Skip validation rules for not available fields in data.
237
+
foreach ( $validation_rules as $key => $value ) {
238
+
if ( ! array_key_exists( $key, $data ) ) {
239
+
unset( $validation_rules[ $key ] );
240
+
}
241
+
}
242
+
243
+
return ValidationHelper::validate( $validation_rules, $data );
244
+
}
245
+
}
246
+