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.0 3.4.1 3.4.2 3.5.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 11 months ago PaymentGateways 1 month ago AdminMenu.php 3 weeks ago BillingController.php 3 weeks ago CartController.php 3 weeks ago CheckoutController.php 3 weeks ago CouponController.php 3 weeks ago Ecommerce.php 3 weeks ago EmailController.php 1 year ago HooksHandler.php 2 weeks ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 3 weeks ago PaymentHandler.php 10 months ago Settings.php 1 week ago Tax.php 10 months ago currency.php 6 months ago
Settings.php
508 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 = self::get_payment_settings();
284
285 if ( tutor_utils()->count( $payments['payment_methods'] ) ) {
286 foreach ( $payments['payment_methods'] as $method ) {
287 if ( $method->name === $gateway ) {
288 return (bool) $method->is_active;
289 }
290 }
291 }
292
293 return false;
294 }
295
296 /**
297 * Get currency options where key is symbol
298 * and code is value
299 *
300 * @since 3.0.0
301 *
302 * @return array
303 */
304 public static function get_currency_options() {
305 $currencies = get_tutor_currencies();
306
307 $options = array();
308
309 foreach ( $currencies as $currency ) {
310 $options[ $currency['code'] ] = $currency['code'] . ' (' . $currency['symbol'] . ')';
311 }
312 return $options;
313 }
314
315 /**
316 * Currency position options
317 *
318 * @since 3.0.0
319 *
320 * @return array
321 */
322 public static function get_currency_position_options() {
323 return array(
324 'left' => __( 'Left', 'tutor' ),
325 'right' => __( 'Right', 'tutor' ),
326 );
327 }
328
329 /**
330 * Get currency options where key is symbol
331 * and code is value
332 *
333 * It will return $ as default
334 *
335 * @since 3.0.0
336 *
337 * @param mixed $code Currency code.
338 *
339 * @return string
340 */
341 public static function get_currency_symbol_by_code( $code ) {
342 $currencies = get_tutor_currencies();
343 $search = array_search( $code, array_column( $currencies, 'code' ) );
344
345 if ( false !== $search ) {
346 return $currencies[ $search ]['symbol'];
347 } else {
348 return '$';
349 }
350 }
351
352 /**
353 * Get payment settings
354 *
355 * @since 3.0.0
356 *
357 * @return array
358 */
359 public static function get_payment_settings() {
360 $settings = tutor_utils()->get_option( OptionKeys::PAYMENT_SETTINGS );
361
362 // `payment_settings` is persisted as JSON, but some nested editor values contain
363 // HTML attributes. WordPress unslashes those quotes during save, so we need to
364 // re-escape attribute-bearing tags before decoding the JSON payload again.
365 $settings = preg_replace_callback(
366 '/<([a-zA-Z][\w:-]*)(\s+[^<>]*?)?>/',
367 function ( $matches ) {
368 return ! empty( $matches[2] ) ? addslashes( $matches[0] ) : $matches[0];
369 },
370 $settings
371 );
372
373 $settings = json_decode( $settings, true );
374
375 return is_array( $settings ) ? $settings : array();
376 }
377
378 /**
379 * Get specific payment gateway settings.
380 *
381 * @since 3.0.0
382 *
383 * @param string $gateway_name gateway name.
384 *
385 * @return array
386 */
387 public static function get_payment_gateway_settings( $gateway_name ) {
388 $settings = self::get_payment_settings();
389
390 if ( empty( $gateway_name ) || ! isset( $settings['payment_methods'] ) || ! is_array( $settings['payment_methods'] ) ) {
391 return array();
392 }
393
394 $data = array_values(
395 array_filter(
396 $settings['payment_methods'],
397 function ( $method ) use ( $gateway_name ) {
398 return $method['name'] === $gateway_name;
399 }
400 )
401 );
402
403 return isset( $data[0] ) ? $data[0] : array();
404 }
405
406 /**
407 * Ajax handler to get payment settings
408 *
409 * @since 3.0.0
410 *
411 * @return void send wp_json
412 */
413 public function ajax_get_tutor_payment_settings() {
414 tutor_utils()->checking_nonce();
415 tutor_utils()->check_current_user_capability();
416
417 $settings = self::get_payment_settings();
418 $this->json_response( __( 'Success', 'tutor' ), $settings );
419 }
420
421 /**
422 * Get tutor pro payment gateways
423 *
424 * @since 3.0.0
425 *
426 * @return void send wp_json response
427 */
428 public function ajax_tutor_payment_gateways() {
429 tutor_utils()->checking_nonce();
430 tutor_utils()->check_current_user_capability();
431
432 try {
433 $payment_gateways = array();
434
435 $default_gateway = array(
436 'name' => 'paypal',
437 'label' => 'PayPal',
438 'is_installed' => true,
439 'is_plugin_active' => true,
440 'is_active' => false,
441 'icon' => tutor()->url . 'assets/images/paypal.svg',
442 'support_subscription' => true,
443 'fields' => self::get_paypal_config_fields(),
444 );
445
446 $payment_gateways[] = $default_gateway;
447
448 $this->json_response( __( 'Success', 'tutor' ), apply_filters( 'tutor_payment_gateways', $payment_gateways ) );
449 } catch ( \Throwable $th ) {
450 $this->json_response( $th->getMessage(), null, HttpHelper::STATUS_BAD_REQUEST );
451 }
452 }
453
454 /**
455 * Get paypal config keys
456 *
457 * @since 3.0.0
458 *
459 * @return array
460 */
461 public static function get_paypal_config_keys() {
462 return array(
463 'environment' => 'select',
464 'merchant_email' => 'text',
465 'client_id' => 'secret_key',
466 'secret_id' => 'secret_key',
467 'webhook_id' => 'secret_key',
468 'webhook_url' => 'webhook_url',
469 );
470 }
471
472 /**
473 * Get config fields
474 *
475 * @since 3.0.0.0
476 *
477 * @return array
478 */
479 public static function get_paypal_config_fields() {
480 $config_keys = self::get_paypal_config_keys();
481 $config_fields = array();
482
483 foreach ( $config_keys as $key => $type ) {
484 if ( 'environment' === $key ) {
485 $config_fields[] = array(
486 'name' => $key,
487 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor' ),//phpcs:ignore
488 'type' => $type,
489 'options' => array(
490 'test' => __( 'Test', 'tutor' ),
491 'live' => __( 'Live', 'tutor' ),
492 ),
493 'value' => 'test',
494 );
495 } else {
496 $config_fields[] = array(
497 'name' => $key,
498 'type' => $type,
499 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor-' ),//phpcs:ignore
500 'value' => '',
501 );
502 }
503 }
504
505 return $config_fields;
506 }
507 }
508