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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
496 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 coupon usage enabled in site checkout.
42 *
43 * @since 3.0.0
44 *
45 * @return boolean
46 */
47 public static function is_coupon_usage_enabled() {
48 return (bool) tutor_utils()->get_option( OptionKeys::IS_COUPON_APPLICABLE, false );
49 }
50
51 /**
52 * Format payment settings data.
53 *
54 * @since 3.0.0
55 *
56 * @param array $option option.
57 *
58 * @return array
59 */
60 public function format_payment_settings_data( $option ) {
61 if ( ! empty( $option['payment_settings'] ) ) {
62 $option['payment_settings'] = wp_unslash( $option['payment_settings'] );
63 }
64
65 return $option;
66 }
67
68 /**
69 * Add ecommerce settings
70 *
71 * @param array $fields Tutor setting fields.
72 *
73 * @return array
74 */
75 public static function add_ecommerce_settings( $fields ) {
76 $pages = tutor_utils()->get_pages();
77
78 $pages_fields = array(
79 array(
80 'key' => CartController::PAGE_ID_OPTION_NAME,
81 'type' => 'select',
82 'label' => __( 'Cart Page', 'tutor' ),
83 'default' => '0',
84 'options' => $pages,
85 'desc' => __( 'Select the page you wish to set as the cart page.', 'tutor' ),
86 'searchable' => true,
87 ),
88 array(
89 'key' => CheckoutController::PAGE_ID_OPTION_NAME,
90 'type' => 'select',
91 'label' => __( 'Checkout Page', 'tutor' ),
92 'default' => '0',
93 'options' => $pages,
94 'desc' => __( 'Select the page to be used as the checkout page.', 'tutor' ),
95 'searchable' => true,
96 ),
97 );
98
99 $basic_settings_blocks = array(
100 'ecommerce_block_currency' => array(
101 'label' => __( 'Currency', 'tutor' ),
102 'slug' => 'ecommerce_currency',
103 'block_type' => 'uniform',
104 'fields' => array(
105 array(
106 'key' => OptionKeys::CURRENCY_CODE,
107 'type' => 'select',
108 'label' => __( 'Currency Symbol', 'tutor' ),
109 'select_options' => false,
110 'options' => self::get_currency_options(),
111 'default' => 'USD',
112 'desc' => __( 'Choose the currency for transactions.', 'tutor' ),
113 'searchable' => true,
114 ),
115 array(
116 'key' => OptionKeys::CURRENCY_POSITION,
117 'type' => 'select',
118 'label' => __( 'Currency Position', 'tutor' ),
119 'select_options' => false,
120 'options' => self::get_currency_position_options(),
121 'default' => 'left',
122 'desc' => __( 'Set the position of the currency symbol.', 'tutor' ),
123 ),
124 array(
125 'key' => OptionKeys::THOUSAND_SEPARATOR,
126 'type' => 'text',
127 'label' => __( 'Thousand Separator', 'tutor' ),
128 'field_classes' => 'tutor-w-90',
129 'default' => ',',
130 'desc' => __( 'Specify the thousand separator.', 'tutor' ),
131 ),
132 array(
133 'key' => OptionKeys::DECIMAL_SEPARATOR,
134 'type' => 'text',
135 'label' => __( 'Decimal Separator', 'tutor' ),
136 'field_classes' => 'tutor-w-90',
137 'default' => '.',
138 'desc' => __( 'Specify the decimal separator.', 'tutor' ),
139 ),
140 array(
141 'key' => OptionKeys::NUMBER_OF_DECIMALS,
142 'type' => 'number',
143 'label' => __( 'Number of Decimals', 'tutor' ),
144 'default' => '2',
145 'desc' => __( 'Set the number of decimal places.', 'tutor' ),
146 ),
147 ),
148 ),
149 );
150
151 foreach ( $pages_fields as $page_field ) {
152 $fields['monetization']['blocks']['block_options']['fields'][] = $page_field;
153 }
154
155 $prepared_blocks = array();
156 foreach ( $fields['monetization']['blocks'] as $key => $block ) {
157 $prepared_blocks[ $key ] = $block;
158 if ( 'block_options' === $key ) {
159 foreach ( $basic_settings_blocks as $key => $block ) {
160 $prepared_blocks[ $key ] = $block;
161 }
162 }
163 }
164
165 $fields['monetization']['blocks'] = $prepared_blocks;
166
167 $arr = apply_filters( 'tutor_before_ecommerce_payment_settings', array() );
168
169 /**
170 * Ecommerce payment settings will be generated from react app.
171 */
172 $arr['ecommerce_payment'] = array(
173 'label' => __( 'Payment Methods', 'tutor' ),
174 'slug' => 'ecommerce_payment',
175 'desc' => __( 'Advanced Settings', 'tutor' ),
176 'template' => 'basic',
177 'icon' => 'tutor-icon-credit-card',
178 'blocks' => array(
179 array(
180 'label' => '',
181 'slug' => 'options',
182 'block_type' => 'uniform',
183 'class' => 'tutor-d-none',
184 'fields' => array(
185 array(
186 'key' => 'payment_settings',
187 'type' => 'text',
188 'label' => __( 'Payment Settings', 'tutor' ),
189 'desc' => '',
190 ),
191 ),
192 ),
193 ),
194 );
195
196 /**
197 * Tax settings will be generated from react app.
198 */
199 $arr['ecommerce_tax'] = array(
200 'label' => __( 'Taxes', 'tutor' ),
201 'slug' => 'ecommerce_tax',
202 'desc' => __( 'Advanced Settings', 'tutor' ),
203 'template' => 'basic',
204 'icon' => 'tutor-icon-receipt-percent',
205 'blocks' => array(
206 array(
207 'label' => '',
208 'slug' => 'options',
209 'block_type' => 'uniform',
210 'class' => 'tutor-d-none',
211 'fields' => array(
212 array(
213 'key' => 'ecommerce_tax',
214 'type' => 'text',
215 'label' => __( 'Tax Settings', 'tutor' ),
216 'desc' => '',
217 ),
218 ),
219 ),
220 ),
221 );
222
223 $arr['ecommerce_checkout'] = array(
224 'label' => __( 'Checkout', 'tutor' ),
225 'slug' => 'ecommerce_checkout',
226 'template' => 'basic',
227 'icon' => 'tutor-icon-change',
228 'blocks' => array(
229 array(
230 'label' => __( 'Checkout Configuration', 'tutor' ),
231 'desc' => __( 'Customize your checkout process to suit your preferences.', 'tutor' ),
232 'slug' => 'checkout_configuration',
233 'block_type' => 'uniform',
234 'fields' => array(
235 array(
236 'key' => OptionKeys::IS_COUPON_APPLICABLE,
237 'type' => 'toggle_switch',
238 'label' => __( 'Enable Coupon Code', 'tutor' ),
239 'default' => 'on',
240 'desc' => __( 'Allow users to apply the coupon code during checkout.', '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 $settings = json_decode( stripslashes( $settings ), true );
363
364 return $settings;
365 }
366
367 /**
368 * Get specific payment gateway settings.
369 *
370 * @since 3.0.0
371 *
372 * @param string $gateway_name gateway name.
373 *
374 * @return array
375 */
376 public static function get_payment_gateway_settings( $gateway_name ) {
377 $settings = self::get_payment_settings();
378
379 if ( empty( $gateway_name ) || ! isset( $settings['payment_methods'] ) || ! is_array( $settings['payment_methods'] ) ) {
380 return array();
381 }
382
383 $data = array_values(
384 array_filter(
385 $settings['payment_methods'],
386 function ( $method ) use ( $gateway_name ) {
387 return $method['name'] === $gateway_name;
388 }
389 )
390 );
391
392 return isset( $data[0] ) ? $data[0] : array();
393 }
394
395 /**
396 * Ajax handler to get payment settings
397 *
398 * @since 3.0.0
399 *
400 * @return void send wp_json
401 */
402 public function ajax_get_tutor_payment_settings() {
403 tutor_utils()->checking_nonce();
404 tutor_utils()->check_current_user_capability();
405
406 $settings = self::get_payment_settings();
407 $this->json_response( __( 'Success', 'tutor' ), $settings );
408 }
409
410 /**
411 * Get tutor pro payment gateways
412 *
413 * @since 3.0.0
414 *
415 * @return void send wp_json response
416 */
417 public function ajax_tutor_payment_gateways() {
418 tutor_utils()->checking_nonce();
419 tutor_utils()->check_current_user_capability();
420
421 try {
422 $payment_gateways = array();
423
424 $default_gateway = array(
425 'name' => 'paypal',
426 'label' => 'PayPal',
427 'is_installed' => true,
428 'is_active' => false,
429 'icon' => tutor()->url . 'assets/images/paypal.svg',
430 'support_subscription' => true,
431 'fields' => self::get_paypal_config_fields(),
432 );
433
434 $payment_gateways[] = $default_gateway;
435
436 $this->json_response( __( 'Success', 'tutor' ), apply_filters( 'tutor_payment_gateways', $payment_gateways ) );
437 } catch ( \Throwable $th ) {
438 $this->json_response( $th->getMessage(), null, HttpHelper::STATUS_BAD_REQUEST );
439 }
440 }
441
442 /**
443 * Get paypal config keys
444 *
445 * @since 3.0.0
446 *
447 * @return array
448 */
449 public static function get_paypal_config_keys() {
450 return array(
451 'environment' => 'select',
452 'merchant_email' => 'text',
453 'client_id' => 'secret_key',
454 'secret_id' => 'secret_key',
455 'webhook_id' => 'secret_key',
456 'webhook_url' => 'webhook_url',
457 );
458 }
459
460 /**
461 * Get config fields
462 *
463 * @since 3.0.0.0
464 *
465 * @return array
466 */
467 public static function get_paypal_config_fields() {
468 $config_keys = self::get_paypal_config_keys();
469 $config_fields = array();
470
471 foreach ( $config_keys as $key => $type ) {
472 if ( 'environment' === $key ) {
473 $config_fields[] = array(
474 'name' => $key,
475 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor' ),//phpcs:ignore
476 'type' => $type,
477 'options' => array(
478 'test' => __( 'Test', 'tutor' ),
479 'live' => __( 'Live', 'tutor' ),
480 ),
481 'value' => 'test',
482 );
483 } else {
484 $config_fields[] = array(
485 'name' => $key,
486 'type' => $type,
487 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor-' ),//phpcs:ignore
488 'value' => '',
489 );
490 }
491 }
492
493 return $config_fields;
494 }
495 }
496