Cart
10 months ago
PaymentGateways
4 weeks 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 |