Cart
10 months ago
PaymentGateways
8 months ago
AdminMenu.php
9 months ago
BillingController.php
1 year ago
CartController.php
1 year ago
CheckoutController.php
7 months ago
CouponController.php
11 months ago
Ecommerce.php
1 year ago
EmailController.php
11 months ago
HooksHandler.php
7 months ago
OptionKeys.php
1 year ago
OrderActivitiesController.php
1 year ago
OrderController.php
7 months ago
PaymentHandler.php
9 months ago
Settings.php
9 months ago
Tax.php
9 months ago
currency.php
1 year ago
Settings.php
497 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 | $settings = json_decode( $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_plugin_active' => true, |
| 429 | 'is_active' => false, |
| 430 | 'icon' => tutor()->url . 'assets/images/paypal.svg', |
| 431 | 'support_subscription' => true, |
| 432 | 'fields' => self::get_paypal_config_fields(), |
| 433 | ); |
| 434 | |
| 435 | $payment_gateways[] = $default_gateway; |
| 436 | |
| 437 | $this->json_response( __( 'Success', 'tutor' ), apply_filters( 'tutor_payment_gateways', $payment_gateways ) ); |
| 438 | } catch ( \Throwable $th ) { |
| 439 | $this->json_response( $th->getMessage(), null, HttpHelper::STATUS_BAD_REQUEST ); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Get paypal config keys |
| 445 | * |
| 446 | * @since 3.0.0 |
| 447 | * |
| 448 | * @return array |
| 449 | */ |
| 450 | public static function get_paypal_config_keys() { |
| 451 | return array( |
| 452 | 'environment' => 'select', |
| 453 | 'merchant_email' => 'text', |
| 454 | 'client_id' => 'secret_key', |
| 455 | 'secret_id' => 'secret_key', |
| 456 | 'webhook_id' => 'secret_key', |
| 457 | 'webhook_url' => 'webhook_url', |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Get config fields |
| 463 | * |
| 464 | * @since 3.0.0.0 |
| 465 | * |
| 466 | * @return array |
| 467 | */ |
| 468 | public static function get_paypal_config_fields() { |
| 469 | $config_keys = self::get_paypal_config_keys(); |
| 470 | $config_fields = array(); |
| 471 | |
| 472 | foreach ( $config_keys as $key => $type ) { |
| 473 | if ( 'environment' === $key ) { |
| 474 | $config_fields[] = array( |
| 475 | 'name' => $key, |
| 476 | 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor' ),//phpcs:ignore |
| 477 | 'type' => $type, |
| 478 | 'options' => array( |
| 479 | 'test' => __( 'Test', 'tutor' ), |
| 480 | 'live' => __( 'Live', 'tutor' ), |
| 481 | ), |
| 482 | 'value' => 'test', |
| 483 | ); |
| 484 | } else { |
| 485 | $config_fields[] = array( |
| 486 | 'name' => $key, |
| 487 | 'type' => $type, |
| 488 | 'label' => __( ucfirst( str_replace( '_', ' ', $key ) ), 'tutor-' ),//phpcs:ignore |
| 489 | 'value' => '', |
| 490 | ); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return $config_fields; |
| 495 | } |
| 496 | } |
| 497 |