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