Settings.php
7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace MeowCrew\RoleAndCustomerBasedPricing\Settings;
use MeowCrew\RoleAndCustomerBasedPricing\Core\ServiceContainerTrait ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\CustomOptions\CustomizerButtonOption ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\CustomOptions\DurationNumberOption ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\CustomOptions\PremiumImportOption ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\CustomOptions\PremiumSelectOption ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\CustomOptions\TemplateOption ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\CustomOptions\SwitchCheckboxOption ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\AbstractSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\CustomizerSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\DebugSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\DisplayPagesSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\EventsListLinkSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\EventsSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\ExcludedRolesSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\ImportExportSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\MainSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\MobileSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\PopupBehaviourSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\PopupTriggerSection ;
use MeowCrew\RoleAndCustomerBasedPricing\Settings\Sections\PricingSection ;
/**
* Class Settings
*
* @package Settings
*/
class Settings
{
use ServiceContainerTrait ;
const SETTINGS_PREFIX = 'role_and_customer_based_pricing_' ;
const SETTINGS_PAGE = 'role_and_customer_based_pricing_settings' ;
/**
* Array with the settings
*
* @var array
*/
private $settings ;
/**
* Sections
*
* @var AbstractSection[]
*/
private $sections ;
/**
* Settings constructor.
*/
public function __construct()
{
$this->initCustomOptions();
$this->initSections();
$this->hooks();
}
public function initCustomOptions()
{
$this->getContainer()->add( 'settings.SwitchCheckboxOption', new SwitchCheckboxOption() );
$this->getContainer()->add( 'settings.TemplateOption', new TemplateOption() );
$this->getContainer()->add( 'settings.PremiumSelectOption', new PremiumSelectOption() );
$this->getContainer()->add( 'settings.PremiumImportOption', new PremiumImportOption() );
}
public function initSections()
{
$this->sections = array(
'main' => new MainSection(),
'pricing' => new PricingSection(),
);
if ( !racbpfw_fs()->is_premium() ) {
$this->sections['import_export'] = new ImportExportSection();
}
}
/**
* Handle updating settings
*/
public function updateSettings()
{
woocommerce_update_options( $this->settings );
}
/**
* Init all settings
*/
public function initSettings()
{
$settings = array();
foreach ( $this->sections as $section ) {
$settings[$section->getName() . '__section'] = array(
'title' => $section->getTitle(),
'desc' => $section->getDescription(),
'id' => self::SETTINGS_PREFIX . $section->getName() . '__section',
'type' => 'title',
);
foreach ( $section->getSettings() as $key => $value ) {
$settings[$key] = $value;
}
$settings[$section->getName() . '__section_end'] = array(
'id' => self::SETTINGS_PREFIX . $section->getName() . '__section_end',
'type' => 'sectionend',
);
}
$this->settings = apply_filters( 'role_customer_specific_pricing/settings/settings', $settings );
}
/**
* Register hooks
*/
public function hooks()
{
if ( !racbpfw_fs()->is_premium() ) {
$template = 'upgrade-alert.php';
add_action( 'woocommerce_settings_' . self::SETTINGS_PAGE, function () use( $template ) {
$this->getContainer()->getFileManager()->includeTemplate( 'admin/alerts/' . $template, [
'upgradeUrl' => racbpfw_fs()->get_upgrade_url(),
'contactUsUrl' => racbpfw_fs()->contact_url(),
] );
} );
}
add_action( 'init', array( $this, 'initSettings' ) );
add_filter( 'woocommerce_settings_tabs_' . self::SETTINGS_PAGE, array( $this, 'registerSettings' ) );
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'addSettingsTab' ), 50 );
add_action( 'woocommerce_update_options_' . self::SETTINGS_PAGE, array( $this, 'updateSettings' ) );
}
/**
* Add own settings tab
*
* @param array $settings_tabs
*
* @return mixed
*/
public function addSettingsTab( $settings_tabs )
{
$settings_tabs[self::SETTINGS_PAGE] = __( 'Role\\Customer Based Pricing', 'role-and-customer-based-pricing-for-woocommerce' );
return $settings_tabs;
}
/**
* Add settings to WooCommerce
*/
public function registerSettings()
{
woocommerce_admin_fields( $this->settings );
}
/**
* Get setting by name
*
* @param string $option_name
* @param mixed $default
*
* @return mixed
*/
public function get( $option_name, $default = null )
{
return get_option( self::SETTINGS_PREFIX . $option_name, $default );
}
public function getAllSettings()
{
$settings = array_filter( $this->settings, function ( $setting ) {
return !in_array( $setting['type'], array(
'section',
'sectionend',
'title',
CustomizerButtonOption::FIELD_TYPE
) );
} );
return array_map( function ( $key, $value ) {
return $this->get( $key, $value['default'] );
}, array_keys( $settings ), $settings );
}
public function doPreventPurchaseForNonLoggedInUsers()
{
return $this->get( 'prevent_purchase_for_non_logged_in_users', 'no' ) === 'yes';
}
public function doHidePricesForNonLoggedInUsers()
{
return $this->get( 'hide_prices_for_non_logged_in_users', 'no' ) === 'yes';
}
public function getNonLoggedInUsersPurchaseMessage()
{
// translators: %s: login page url
return $this->get( 'non_logged_in_users_purchase_message', sprintf( __( 'Please enter %s to make a purchase', 'role-and-customer-based-pricing-for-woocommerce' ), sprintf( '<a href="%s">%s</a>', wc_get_account_endpoint_url( 'dashboard' ), __( 'your account', 'role-and-customer-based-pricing-for-woocommerce' ) ) ) );
}
public function getNonLoggedInUsersAddToCartButtonLabel()
{
return $this->get( 'add_to_cart_label_for_non_logged_in_users', __( 'Only for registered clients', 'role-and-customer-based-pricing-for-woocommerce' ) );
}
public function isDebugEnabled()
{
return $this->get( 'debug', 'no' ) === 'yes';
}
public function getPercentageBasedRulesBehavior()
{
if ( !racbpfw_fs()->is_premium() ) {
return 'full_price';
}
return $this->get( 'percentage_based_pricing_rule_behaviour', 'full_price' );
}
/**
* Get url to settings page
*
* @return string
*/
public function getLink()
{
return admin_url( 'admin.php?page=wc-settings&tab=' . self::SETTINGS_PAGE );
}
public function isSettingsPage()
{
return isset( $_GET['tab'] ) && self::SETTINGS_PAGE === $_GET['tab'];
}
}