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.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / BillingController.php
tutor / ecommerce Last commit date
Cart 10 months ago PaymentGateways 1 month ago AdminMenu.php 1 day ago BillingController.php 1 day ago CartController.php 1 day ago CheckoutController.php 1 day ago CouponController.php 1 day ago Ecommerce.php 1 day ago EmailController.php 11 months ago HooksHandler.php 1 day ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 1 day ago PaymentHandler.php 9 months ago Settings.php 1 day ago Tax.php 9 months ago currency.php 5 months ago
BillingController.php
326 lines
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 stdClass;
14 use TUTOR\Icon;
15 use TUTOR\BaseController;
16 use Tutor\Helpers\HttpHelper;
17 use Tutor\Helpers\ValidationHelper;
18 use Tutor\Models\BillingModel;
19 use Tutor\Traits\JsonResponse;
20
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit;
23 }
24
25 /**
26 * BillingController class
27 *
28 * @since 3.0.0
29 */
30 class BillingController extends BaseController {
31
32
33 /**
34 * Billing model
35 *
36 * @since 3.0.0
37 *
38 * @var BillingModel
39 */
40 private $model;
41
42 /**
43 * Trait for sending JSON response
44 */
45 use JsonResponse;
46
47 /**
48 * Constructor.
49 *
50 * Initializes the Billing class, sets the page title, and optionally registers
51 * hooks for handling AJAX requests related to billing data.
52 *
53 * @param bool $register_hooks Whether to register hooks for handling requests. Default is true.
54 *
55 * @since 3.0.0
56 *
57 * @return void
58 */
59 public function __construct( $register_hooks = true ) {
60 $this->model = new BillingModel();
61
62 if ( $register_hooks ) {
63 add_filter( 'tutor_dashboard/nav_items/settings/nav_items', array( $this, 'register_nav' ) );
64 add_filter( 'load_dashboard_template_part_from_other_location', array( $this, 'load_template' ) );
65
66 /**
67 * Handle AJAX request for saving billing info if current user.
68 *
69 * @since 3.0.0
70 */
71 add_action( 'wp_ajax_tutor_save_billing_info', array( $this, 'save_billing_info' ) );
72
73 /**
74 * Handle AJAX request for getting billing info if current user.
75 *
76 * @since 3.0.0
77 */
78 add_action( 'wp_ajax_tutor_get_billing_info', array( $this, 'get_billing_info' ) );
79 }
80 }
81
82 /**
83 * Register billing nav menu for settings
84 *
85 * @since 3.0.0
86 * @since 4.0.0 update tabs order and data structure.
87 *
88 * @param array $tabs setting navigation tabs.
89 *
90 * @return array
91 */
92 public static function register_nav( $tabs ) {
93 $id = 'billing-address';
94
95 $new_tab = array(
96 'id' => $id,
97 'label' => 'Billing Address',
98 'icon' => Icon::BILLING,
99 'text' => __( 'Your payment address', 'tutor' ),
100 'template' => 'ecommerce.billing',
101 'role' => false,
102 );
103
104 $position = array_search( 'social-accounts', array_keys( $tabs ), true );
105
106 if ( false === $position ) {
107 $tabs[ $id ] = $new_tab;
108 return $tabs;
109 }
110
111 return array_slice( $tabs, 0, $position + 1, true )
112 + array( $id => $new_tab )
113 + array_slice( $tabs, $position + 1, null, true );
114 }
115
116 /**
117 * Load billing template for settings
118 *
119 * Based on query_vars filter template path
120 *
121 * @since 3.0.0
122 *
123 * @param string $location default file location.
124 *
125 * @return string
126 */
127 public static function load_template( $location ) {
128 $page_name = get_query_var( 'pagename' );
129 $dashboard_sub_page = get_query_var( 'tutor_dashboard_sub_page' );
130
131 $dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
132 $dashboard_page = get_post( $dashboard_page_id );
133
134 // Current page is dashboard & sub page is billing.
135 if ( $page_name === $dashboard_page->post_name && 'billing' === $dashboard_sub_page ) {
136 $template = tutor()->path . 'templates/ecommerce/billing.php';
137
138 if ( file_exists( $template ) ) {
139 $location = $template;
140 }
141 }
142
143 return $location;
144 }
145
146 /**
147 * Get the customer model object
148 *
149 * @since 3.0.0
150 *
151 * @return object
152 */
153 public function get_model() {
154 return $this->model;
155 }
156
157 /**
158 * Save billing information for the current user.
159 *
160 * @since 3.0.0
161 *
162 * @return void
163 */
164 public function save_billing_info() {
165 if ( ! tutor_utils()->is_nonce_verified() ) {
166 $this->json_response(
167 tutor_utils()->error_message( 'nonce' ),
168 null,
169 HttpHelper::STATUS_BAD_REQUEST
170 );
171 }
172
173 $data = $this->get_allowed_fields( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
174
175 $user_id = get_current_user_id();
176 $data['user_id'] = $user_id;
177
178 $validation = $this->validate( $data );
179 if ( ! $validation->success ) {
180 $this->json_response(
181 __( 'Invalid inputs', 'tutor' ),
182 $validation->errors,
183 HttpHelper::STATUS_UNPROCESSABLE_ENTITY
184 );
185 }
186
187 $billing_info = $this->get_billing_info();
188
189 if ( $billing_info ) {
190 $response = $this->model->update( $data, array( 'user_id' => $user_id ) );
191 } else {
192 $response = $this->model->insert( $data );
193 }
194
195 if ( ! $response ) {
196 $this->json_response(
197 __( 'Failed to save billing info', 'tutor' ),
198 null,
199 HttpHelper::STATUS_INTERNAL_SERVER_ERROR
200 );
201 }
202
203 $this->json_response( __( 'Billing info saved successfully', 'tutor' ) );
204 }
205
206 /**
207 * Get billing information for the current user.
208 *
209 * @since 3.0.0
210 *
211 * @param int $user_id User id.
212 *
213 * @return mixed The user's billing information.
214 */
215 public function get_billing_info( $user_id = 0 ) {
216 $user_id = tutor_utils()->get_user_id( $user_id );
217 return $this->model->get_info( $user_id );
218 }
219
220 /**
221 * Validate input data based on predefined rules.
222 *
223 * This protected method validates the provided data array against a set of
224 * predefined validation rules. The rules specify that 'order_id' is required
225 * and must be numeric. The method will skip validation rules for fields that
226 * are not present in the data array.
227 *
228 * @since 3.0.0
229 *
230 * @param array $data The data array to validate.
231 *
232 * @return object The validation result. It returns validation object.
233 */
234 protected function validate( array $data ) {
235
236 $validation_rules = array(
237 'user_id' => 'required|numeric',
238 'first_name' => 'required',
239 'last_name' => 'required',
240 'email' => 'required|email',
241 'phone' => 'required',
242 'zip_code' => 'required',
243 'address' => 'required',
244 'country' => 'required',
245 'state' => 'required',
246 'city' => 'required',
247 );
248
249 // Skip validation rules for not available fields in data.
250 foreach ( $validation_rules as $key => $value ) {
251 if ( ! array_key_exists( $key, $data ) ) {
252 unset( $validation_rules[ $key ] );
253 }
254 }
255
256 return ValidationHelper::validate( $validation_rules, $data );
257 }
258
259 /**
260 * Get country and state options for billing information.
261 *
262 * @since 4.0.0
263 *
264 * @return object An object containing country and state options.
265 */
266 public static function get_country_state_options() {
267 $countries = tutor_get_country_list();
268 $country_options = array();
269 $state_mapping = array();
270
271 foreach ( $countries as $country ) {
272 array_push(
273 $country_options,
274 array(
275 'label' => $country['name'],
276 'value' => $country['name'],
277 )
278 );
279
280 if ( ! empty( $country['states'] ) ) {
281 $state_mapping[ $country['name'] ] = array_map(
282 function ( $state ) {
283 return array(
284 'label' => $state['name'],
285 'value' => $state['name'],
286 );
287 },
288 $country['states']
289 );
290 }
291 }
292
293 $obj = new stdClass();
294 $obj->country_options = $country_options;
295 $obj->state_options = $state_mapping;
296
297 return $obj;
298 }
299
300 /**
301 * Get default values for billing form.
302 *
303 * @since 4.0.0
304 *
305 * @return array An array containing default values for billing information.
306 */
307 public static function get_default_values() {
308 $billing_info = ( new BillingController() )->get_billing_info();
309 $billing_country = $billing_info->billing_country ?? tutor_utils()->input_old( 'billing_country', '' );
310
311 $default_values = array(
312 'billing_first_name' => $billing_info->billing_first_name ?? '',
313 'billing_last_name' => $billing_info->billing_last_name ?? '',
314 'billing_email' => $billing_info->billing_email ?? '',
315 'billing_country' => $billing_country,
316 'billing_state' => $billing_info->billing_state ?? '',
317 'billing_city' => $billing_info->billing_city ?? '',
318 'billing_phone' => $billing_info->billing_phone ?? '',
319 'billing_zip_code' => $billing_info->billing_zip_code ?? '',
320 'billing_address' => $billing_info->billing_address ?? '',
321 );
322
323 return $default_values;
324 }
325 }
326