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.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 / Settings.php
tutor / ecommerce Last commit date
PaymentGateways 1 year ago AdminMenu.php 1 year ago BillingController.php 1 year ago CartController.php 1 year ago CheckoutController.php 1 year ago CouponController.php 1 year ago Ecommerce.php 1 year ago EmailController.php 1 year ago HooksHandler.php 1 year ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 1 year ago PaymentHandler.php 1 year ago Settings.php 1 year ago Tax.php 1 year ago currency.php 1 year ago
Settings.php
514 lines
1 <?php
2 /**
3 * Settings class for configuring ecommerce settings
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\Helpers\HttpHelper;
14 use TUTOR\Input;
15 use Tutor\PaymentGateways\Configs\PaypalConfig;
16 use Tutor\Traits\JsonResponse;
17
18 /**
19 * Configure ecommerce settings
20 */
21 class Settings {
22
23 use JsonResponse;
24
25 /**
26 * Register hooks
27 */
28 public function __construct() {
29 add_filter( 'tutor/options/extend/attr', __CLASS__ . '::add_ecommerce_settings' );
30 add_action( 'add_manual_payment_btn', __CLASS__ . '::add_manual_payment_btn' );
31 add_action( 'wp_ajax_tutor_add_manual_payment_method', __CLASS__ . '::ajax_add_manual_payment_method' );
32 add_action( 'wp_ajax_tutor_delete_manual_payment_method', __CLASS__ . '::ajax_delete_manual_payment_method' );
33
34 add_filter( 'tutor_option_input', array( $this, 'format_payment_settings_data' ) );
35 add_action( 'wp_ajax_tutor_payment_settings', array( $this, 'ajax_get_tutor_payment_settings' ) );
36 add_action( 'wp_ajax_tutor_payment_gateways', array( $this, 'ajax_tutor_payment_gateways' ) );
37 }
38
39
40 /**
41 * Check if buy now setting is enabled.
42 *
43 * @since 3.4.0
44 *
45 * @return boolean
46 */
47 public static function is_buy_now_enabled() {
48 return (bool) tutor_utils()->get_option( OptionKeys::BUY_NOW, false );
49 }
50
51 /**
52 * Check coupon usage enabled in site checkout.
53 *
54 * @since 3.0.0
55 *
56 * @return boolean
57 */
58 public static function is_coupon_usage_enabled() {
59 return (bool) tutor_utils()->get_option( OptionKeys::IS_COUPON_APPLICABLE, false );
60 }
61
62 /**
63 * Format payment settings data.
64 *
65 * @since 3.0.0
66 *
67 * @param array $option option.
68 *
69 * @return array
70 */
71 public function format_payment_settings_data( $option ) {
72 if ( ! empty( $option['payment_settings'] ) ) {
73 $option['payment_settings'] = wp_unslash( $option['payment_settings'] );
74 }
75
76 return $option;
77 }
78
79 /**
80 * Add ecommerce settings
81 *
82 * @param array $fields Tutor setting fields.
83 *
84 * @return array
85 */
86 public static function add_ecommerce_settings( $fields ) {
87 $pages = tutor_utils()->get_pages();
88
89 $pages_fields = array(
90 array(
91 'key' => CartController::PAGE_ID_OPTION_NAME,
92 'type' => 'select',
93 'label' => __( 'Cart Page', 'tutor' ),
94 'default' => '0',
95 'options' => $pages,
96 'desc' => __( 'Select the page you wish to set as the cart page.', 'tutor' ),
97 'searchable' => true,
98 ),
99 array(
100 'key' => CheckoutController::PAGE_ID_OPTION_NAME,
101 'type' => 'select',
102 'label' => __( 'Checkout Page', 'tutor' ),
103 'default' => '0',
104 'options' => $pages,
105 'desc' => __( 'Select the page to be used as the checkout page.', 'tutor' ),
106 'searchable' => true,
107 ),
108 );
109
110 $basic_settings_blocks = array(
111 'ecommerce_block_currency' => array(
112 'label' => __( 'Currency', 'tutor' ),
113 'slug' => 'ecommerce_currency',
114 'block_type' => 'uniform',
115 'fields' => array(
116 array(
117 'key' => OptionKeys::CURRENCY_CODE,
118 'type' => 'select',
119 'label' => __( 'Currency', 'tutor' ),
120 'select_options' => false,
121 'options' => self::get_currency_options(),
122 'default' => 'USD',
123 'desc' => __( 'Choose the currency for transactions.', 'tutor' ),
124 'searchable' => true,
125 ),
126 array(
127 'key' => OptionKeys::CURRENCY_POSITION,
128 'type' => 'select',
129 'label' => __( 'Currency Position', 'tutor' ),
130 'select_options' => false,
131 'options' => self::get_currency_position_options(),
132 'default' => 'left',
133 'desc' => __( 'Set the position of the currency symbol.', 'tutor' ),
134 ),
135 array(
136 'key' => OptionKeys::THOUSAND_SEPARATOR,
137 'type' => 'text',
138 'label' => __( 'Thousand Separator', 'tutor' ),
139 'field_classes' => 'tutor-w-90',
140 'default' => ',',
141 'desc' => __( 'Specify the thousand separator.', 'tutor' ),
142 ),
143 array(
144 'key' => OptionKeys::DECIMAL_SEPARATOR,
145 'type' => 'text',
146 'label' => __( 'Decimal Separator', 'tutor' ),
147 'field_classes' => 'tutor-w-90',
148 'default' => '.',
149 'desc' => __( 'Specify the decimal separator.', 'tutor' ),
150 ),
151 array(
152 'key' => OptionKeys::NUMBER_OF_DECIMALS,
153 'type' => 'number',
154 'label' => __( 'Number of Decimals', 'tutor' ),
155 'default' => '2',
156 'desc' => __( 'Set the number of decimal places.', 'tutor' ),
157 ),
158 ),
159 ),
160 );
161
162 foreach ( $pages_fields as $page_field ) {
163 $fields['monetization']['blocks']['block_options']['fields'][] = $page_field;
164 }
165
166 $prepared_blocks = array();
167 foreach ( $fields['monetization']['blocks'] as $key => $block ) {
168 $prepared_blocks[ $key ] = $block;
169 if ( 'block_options' === $key ) {
170 foreach ( $basic_settings_blocks as $key => $block ) {
171 $prepared_blocks[ $key ] = $block;
172 }
173 }
174 }
175
176 $fields['monetization']['blocks'] = $prepared_blocks;
177
178 $arr = apply_filters( 'tutor_before_ecommerce_payment_settings', array() );
179
180 /**
181 * Ecommerce payment settings will be generated from react app.
182 */
183 $arr['ecommerce_payment'] = array(
184 'label' => __( 'Payment Methods', 'tutor' ),
185 'slug' => 'ecommerce_payment',
186 'desc' => __( 'Advanced Settings', 'tutor' ),
187 'template' => 'basic',
188 'icon' => 'tutor-icon-credit-card',
189 'blocks' => array(
190 array(
191 'label' => '',
192 'slug' => 'options',
193 'block_type' => 'uniform',
194 'class' => 'tutor-d-none',
195 'fields' => array(
196 array(
197 'key' => 'payment_settings',
198 'type' => 'text',
199 'label' => __( 'Payment Settings', 'tutor' ),
200 'desc' => '',
201 ),
202 ),
203 ),
204 ),
205 );
206
207 /**
208 * Tax settings will be generated from react app.
209 */
210 $arr['ecommerce_tax'] = array(
211 'label' => __( 'Taxes', 'tutor' ),
212 'slug' => 'ecommerce_tax',
213 'desc' => __( 'Advanced Settings', 'tutor' ),
214 'template' => 'basic',
215 'icon' => 'tutor-icon-receipt-percent',
216 'blocks' => array(
217 array(
218 'label' => '',
219 'slug' => 'options',
220 'block_type' => 'uniform',
221 'class' => 'tutor-d-none',
222 'fields' => array(
223 array(
224 'key' => 'ecommerce_tax',
225 'type' => 'text',
226 'label' => __( 'Tax Settings', 'tutor' ),
227 'desc' => '',
228 ),
229 ),
230 ),
231 ),
232 );
233
234 $arr['ecommerce_checkout'] = array(
235 'label' => __( 'Checkout', 'tutor' ),
236 'slug' => 'ecommerce_checkout',
237 'template' => 'basic',
238 'icon' => 'tutor-icon-change',
239 'blocks' => array(
240 array(
241 'label' => __( 'Checkout Configuration', 'tutor' ),
242 'desc' => __( 'Customize your checkout process to suit your preferences.', 'tutor' ),
243 'slug' => 'checkout_configuration',
244 'block_type' => 'uniform',
245 'fields' => array(
246 array(
247 'key' => OptionKeys::IS_COUPON_APPLICABLE,
248 'type' => 'toggle_switch',
249 'label' => __( 'Enable Coupon Code', 'tutor' ),
250 'default' => 'on',
251 'desc' => __( 'Allow users to apply the coupon code during checkout.', 'tutor' ),
252 ),
253 array(
254 'key' => OptionKeys::BUY_NOW,
255 'type' => 'toggle_switch',
256 'label' => __( 'Enable "Buy Now" Button', 'tutor' ),
257 'default' => 'off',
258 'desc' => __( 'Allow users to purchase courses directly without adding them to the cart.', 'tutor' ),
259 ),
260 ),
261 ),
262 ),
263 );
264
265 $arr = apply_filters( 'tutor_after_ecommerce_settings', $arr );
266 $fields['monetization']['submenu'] = $arr;
267
268 return $fields;
269 }
270
271 /**
272 * Get default automate payment gateways
273 *
274 * @since 3.0.0
275 *
276 * @return array
277 */
278 public static function get_default_automate_payment_gateways() {
279 $gateways = array(
280 'paypal' => array(
281 'label' => 'PayPal',
282 'is_active' => self::is_active( 'paypal' ),
283 'icon' => esc_url_raw( tutor()->url . 'assets/images/paypal.svg' ),
284 'support_subscription' => true,
285 ),
286 );
287
288 return apply_filters( 'tutor_default_automate_payment_gateways', $gateways );
289 }
290
291 /**
292 * Check if a payment gateways is active
293 *
294 * @since 3.0.0
295 *
296 * @param string $gateway Gateway key.
297 *
298 * @return boolean
299 */
300 public static function is_active( string $gateway ) : bool {
301 $payments = tutor_utils()->get_option( OptionKeys::PAYMENT_SETTINGS );
302 $payments = json_decode( stripslashes( $payments ) );
303
304 if ( $payments ) {
305 foreach ( $payments->payment_methods as $method ) {
306 if ( $method->name === $gateway ) {
307 return (bool) $method->is_active;
308 }
309 }
310 }
311
312 return false;
313 }
314
315 /**
316 * Get currency options where key is symbol
317 * and code is value
318 *
319 * @since 3.0.0
320 *
321 * @return array
322 */
323 public static function get_currency_options() {
324 $currencies = get_tutor_currencies();
325
326 $options = array();
327
328 foreach ( $currencies as $currency ) {
329 $options[ $currency['code'] ] = $currency['code'] . ' (' . $currency['symbol'] . ')';
330 }
331 return $options;
332 }
333
334 /**
335 * Currency position options
336 *
337 * @since 3.0.0
338 *
339 * @return array
340 */
341 public static function get_currency_position_options() {
342 return array(
343 'left' => __( 'Left', 'tutor' ),
344 'right' => __( 'Right', 'tutor' ),
345 );
346 }
347
348 /**
349 * Get currency options where key is symbol
350 * and code is value
351 *
352 * It will return $ as default
353 *
354 * @since 3.0.0
355 *
356 * @param mixed $code Currency code.
357 *
358 * @return string
359 */
360 public static function get_currency_symbol_by_code( $code ) {
361 $currencies = get_tutor_currencies();
362 $search = array_search( $code, array_column( $currencies, 'code' ) );
363
364 if ( false !== $search ) {
365 return $currencies[ $search ]['symbol'];
366 } else {
367 return '$';
368 }
369 }
370
371 /**
372 * Get payment settings
373 *
374 * @since 3.0.0
375 *
376 * @return object
377 */
378 public static function get_payment_settings() {
379 $settings = tutor_utils()->get_option( OptionKeys::PAYMENT_SETTINGS );
380 $settings = json_decode( stripslashes( $settings ), true );
381
382 return $settings;
383 }
384
385 /**
386 * Get specific payment gateway settings.
387 *
388 * @since 3.0.0
389 *
390 * @param string $gateway_name gateway name.
391 *
392 * @return array
393 */
394 public static function get_payment_gateway_settings( $gateway_name ) {
395 $settings = self::get_payment_settings();
396
397 if ( empty( $gateway_name ) || ! isset( $settings['payment_methods'] ) || ! is_array( $settings['payment_methods'] ) ) {
398 return array();
399 }
400
401 $data = array_values(
402 array_filter(
403 $settings['payment_methods'],
404 function ( $method ) use ( $gateway_name ) {
405 return $method['name'] === $gateway_name;
406 }
407 )
408 );
409
410 return isset( $data[0] ) ? $data[0] : array();
411 }
412
413 /**
414 * Ajax handler to get payment settings
415 *
416 * @since 3.0.0
417 *
418 * @return void send wp_json
419 */
420 public function ajax_get_tutor_payment_settings() {
421 tutor_utils()->checking_nonce();
422 tutor_utils()->check_current_user_capability();
423
424 $settings = self::get_payment_settings();
425 $this->json_response( __( 'Success', 'tutor' ), $settings );
426 }
427
428 /**
429 * Get tutor pro payment gateways
430 *
431 * @since 3.0.0
432 *
433 * @return void send wp_json response
434 */
435 public function ajax_tutor_payment_gateways() {
436 tutor_utils()->checking_nonce();
437 tutor_utils()->check_current_user_capability();
438
439 try {
440 $payment_gateways = array();
441
442 $default_gateway = array(
443 'name' => 'paypal',
444 'label' => 'PayPal',
445 'is_installed' => true,
446 'is_active' => false,
447 'icon' => tutor()->url . 'assets/images/paypal.svg',
448 'support_subscription' => true,
449 'fields' => self::get_paypal_config_fields(),
450 );
451
452 $payment_gateways[] = $default_gateway;
453
454 $this->json_response( __( 'Success', 'tutor' ), apply_filters( 'tutor_payment_gateways', $payment_gateways ) );
455 } catch ( \Throwable $th ) {
456 $this->json_response( $th->getMessage(), null, HttpHelper::STATUS_BAD_REQUEST );
457 }
458 }
459
460 /**
461 * Get paypal config keys
462 *
463 * @since 3.0.0
464 *
465 * @return array
466 */
467 public static function get_paypal_config_keys() {
468 return array(
469 'environment' => 'select',
470 'merchant_email' => 'text',
471 'client_id' => 'secret_key',
472 'secret_id' => 'secret_key',
473 'webhook_id' => 'secret_key',
474 'webhook_url' => 'webhook_url',
475 );
476 }
477
478 /**
479 * Get config fields
480 *
481 * @since 3.0.0.0
482 *
483 * @return array
484 */
485 public static function get_paypal_config_fields() {
486 $config_keys = self::get_paypal_config_keys();
487 $config_fields = array();
488
489 foreach ( $config_keys as $key => $type ) {
490 if ( 'environment' === $key ) {
491 $config_fields[] = array(
492 'name' => $key,
493 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor' ),//phpcs:ignore
494 'type' => $type,
495 'options' => array(
496 'test' => __( 'Test', 'tutor' ),
497 'live' => __( 'Live', 'tutor' ),
498 ),
499 'value' => 'test',
500 );
501 } else {
502 $config_fields[] = array(
503 'name' => $key,
504 'type' => $type,
505 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor-' ),//phpcs:ignore
506 'value' => '',
507 );
508 }
509 }
510
511 return $config_fields;
512 }
513 }
514