| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Settings_Payment |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Admin/Classes |
| 7 |
* @version 4.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
use LearnPress\Helpers\Config; |
| 11 |
use LearnPress\TemplateHooks\Admin\AdminSettingsPage; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class LP_Settings_Payments |
| 17 |
* |
| 18 |
* Manage all payments are registered and settings to control order process. |
| 19 |
*/ |
| 20 |
class LP_Settings_Payments extends LP_Abstract_Settings_Page { |
| 21 |
/** |
| 22 |
* Constructor |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
$this->id = 'payments'; |
| 26 |
$this->text = esc_html__( 'Payments', 'learnpress' ); |
| 27 |
|
| 28 |
parent::__construct(); |
| 29 |
|
| 30 |
add_filter( 'learn-press/admin/submenu-section-title', array( $this, 'custom_section_title' ), 10, 2 ); |
| 31 |
} |
| 32 |
|
| 33 |
public function custom_section_title( $title, $slug ) { |
| 34 |
$sections = $this->get_sections(); |
| 35 |
|
| 36 |
if ( ! empty( $sections[ $slug ] ) && $sections[ $slug ] instanceof LP_Gateway_Abstract ) { |
| 37 |
$title = $title . sprintf( '<span class="learn-press-tooltip" data-tooltip="%s"></span>', esc_attr( $sections[ $slug ]->get_method_description() ) ); |
| 38 |
} |
| 39 |
|
| 40 |
return $title; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get sections. |
| 45 |
* Add a general section and each registered payment is a section. |
| 46 |
* |
| 47 |
* @return mixed |
| 48 |
*/ |
| 49 |
public function get_sections() { |
| 50 |
static $sections; |
| 51 |
if ( is_null( $sections ) ) { |
| 52 |
$gateways = LP_Gateways::instance()->get_gateways(); |
| 53 |
$sections = array( |
| 54 |
'general' => esc_html__( 'General', 'learnpress' ), |
| 55 |
'refund' => esc_html__( 'Refund', 'learnpress' ), |
| 56 |
); |
| 57 |
|
| 58 |
if ( $gateways ) { |
| 59 |
foreach ( $gateways as $id => $gateway ) { |
| 60 |
$sections[ $id ] = $gateway; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $sections; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Settings fields of general section. |
| 70 |
* |
| 71 |
* @return mixed |
| 72 |
*/ |
| 73 |
public function get_settings_general() { |
| 74 |
$checkout_url = learn_press_get_checkout_url(); |
| 75 |
|
| 76 |
$guest = apply_filters( |
| 77 |
'learn-press/payment-settings/general', |
| 78 |
array( |
| 79 |
array( |
| 80 |
'type' => 'title', |
| 81 |
), |
| 82 |
array( |
| 83 |
'title' => esc_html__( 'Guest checkout', 'learnpress' ), |
| 84 |
'id' => 'guest_checkout', |
| 85 |
'default' => 'no', |
| 86 |
'type' => 'checkbox', |
| 87 |
'desc' => esc_html__( 'Enable guest checkout.', 'learnpress' ), |
| 88 |
), |
| 89 |
array( |
| 90 |
'title' => esc_html__( 'Account login', 'learnpress' ), |
| 91 |
'id' => 'enable_login_checkout', |
| 92 |
'default' => 'yes', |
| 93 |
'type' => 'checkbox', |
| 94 |
'desc' => esc_html__( 'Enable the login form in the checkout.', 'learnpress' ), |
| 95 |
), |
| 96 |
array( |
| 97 |
'title' => esc_html__( 'Account creation', 'learnpress' ), |
| 98 |
'id' => 'enable_registration_checkout', |
| 99 |
'default' => 'yes', |
| 100 |
'type' => 'checkbox', |
| 101 |
'desc' => esc_html__( 'Enable the register form in the checkout.', 'learnpress' ), |
| 102 |
), |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
$enpoints = apply_filters( |
| 107 |
'learn-press/payment-settings/checkout-endpoints', |
| 108 |
array( |
| 109 |
array( |
| 110 |
'title' => esc_html__( 'Custom order slug', 'learnpress' ), |
| 111 |
'id' => 'checkout_endpoints[lp_order_received]', |
| 112 |
'default' => 'lp-order-received', |
| 113 |
'placeholder' => 'lp-order-received', |
| 114 |
'type' => 'text', |
| 115 |
'desc' => sprintf( 'e.g. %s', "{$checkout_url}<code>" . LP_Settings::instance()->get( 'checkout_endpoints.lp_order_received', 'lp-order-received' ) . '</code>' ), |
| 116 |
), |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
$payment = array( |
| 121 |
array( |
| 122 |
'title' => esc_html__( 'Payments', 'learnpress' ), |
| 123 |
'id' => 'payment_order', |
| 124 |
'default' => '', |
| 125 |
'type' => 'payment-order', |
| 126 |
), |
| 127 |
array( |
| 128 |
'type' => 'sectionend', |
| 129 |
), |
| 130 |
); |
| 131 |
|
| 132 |
return apply_filters( 'learn-press/payment-settings', array_merge( $guest, $enpoints, $payment ) ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Settings fields of refund section. |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
public function get_settings_refund() { |
| 141 |
return Config::instance()->get( 'refund', 'settings/payments' ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Keep refund option names compatible with the former general settings fields. |
| 146 |
* |
| 147 |
* @param string $name Field name. |
| 148 |
* |
| 149 |
* @return mixed |
| 150 |
*/ |
| 151 |
public function get_admin_field_name( $name ) { |
| 152 |
$tab = AdminSettingsPage::instance()->get_active_tab(); |
| 153 |
$section = AdminSettingsPage::instance()->get_active_section(); |
| 154 |
|
| 155 |
if ( 'payments' === $tab && 'refund' === $section ) { |
| 156 |
if ( empty( $name ) ) { |
| 157 |
$name = md5( microtime( true ) ); |
| 158 |
} |
| 159 |
|
| 160 |
return apply_filters( 'learn_press_settings_field_name_' . $name, "learn_press_{$name}" ); |
| 161 |
} |
| 162 |
|
| 163 |
return parent::get_admin_field_name( $name ); |
| 164 |
} |
| 165 |
|
| 166 |
public function admin_page_settings( $section = null, $tab = null ) { |
| 167 |
$tab = AdminSettingsPage::instance()->get_active_tab(); |
| 168 |
$section = AdminSettingsPage::instance()->get_active_section(); |
| 169 |
$sections = AdminSettingsPage::instance()->get_sections(); |
| 170 |
$section_data = ! empty( $sections[ $section ] ) ? $sections[ $section ] : false; |
| 171 |
|
| 172 |
if ( $section_data instanceof LP_Abstract_Settings ) { |
| 173 |
$section_data->admin_option_settings(); |
| 174 |
} elseif ( is_callable( array( $this, 'admin_options_' . $section ) ) ) { |
| 175 |
call_user_func_array( array( $this, 'admin_options_' . $section ), array( $section, $tab ) ); |
| 176 |
} else { |
| 177 |
do_action( 'learn-press/admin/setting-payments/admin-options-' . $section, $tab ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Output admin option of general page. |
| 183 |
* |
| 184 |
* @param string $section |
| 185 |
* @param string $tab |
| 186 |
*/ |
| 187 |
public function admin_options_general( $section, $tab ) { |
| 188 |
parent::admin_page_settings( $section, $tab ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Output admin option of refund page. |
| 193 |
* |
| 194 |
* @param string $section |
| 195 |
* @param string $tab |
| 196 |
*/ |
| 197 |
public function admin_options_refund( $section, $tab ) { |
| 198 |
parent::admin_page_settings( $section, $tab ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return new LP_Settings_Payments(); |
| 203 |
|