PluginProbe
Tiered Pricing Table for WooCommerce / 6.4.0
Tiered Pricing Table for WooCommerce v6.4.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Settings / Settings.php

Settings.php in Tiered Pricing Table for WooCommerce 6.4.0, at src/Settings/Settings.php

327 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Settings;
4
5 use TierPricingTable\Core\ServiceContainerTrait;
6 use TierPricingTable\Settings\CustomOptions\TPTDisplayType;
7 use TierPricingTable\Settings\CustomOptions\TPTLinkButton;
8 use TierPricingTable\Settings\CustomOptions\TPTTableColumnsField;
9 use TierPricingTable\Settings\CustomOptions\TPTQuantityMeasurementField;
10 use TierPricingTable\Settings\CustomOptions\TPTFeatureFlagOption;
11 use TierPricingTable\Settings\CustomOptions\TPTIntegrationOption;
12 use TierPricingTable\Settings\CustomOptions\TPTSwitchOption;
13 use TierPricingTable\Settings\CustomOptions\TPTTextTemplate;
14 use TierPricingTable\Settings\Sections\Advanced\AdvancedSection;
15 use TierPricingTable\Settings\Sections\CalculationLogic\CalculationLogic;
16 use TierPricingTable\Settings\Sections\GeneralSection\GeneralSection;
17 use TierPricingTable\Settings\Sections\Integrations\IntegrationSection;
18 use TierPricingTable\Settings\Sections\SectionAbstract;
19 use TierPricingTable\TierPricingTablePlugin;
20 /**
21 * Class Settings
22 *
23 * @package TierPricingTable\Settings
24 */
25 class Settings {
26 use ServiceContainerTrait;
27 const SETTINGS_PREFIX = 'tier_pricing_table_';
28
29 const SETTINGS_PAGE = 'tiered_pricing_table_settings';
30
31 const DEFAULT_SECTION = 'general';
32
33 /**
34 * Settings
35 *
36 * @var array
37 */
38 private $settings = array();
39
40 /**
41 * Settings sections
42 *
43 * @var SectionAbstract[]
44 */
45 protected $sections = array();
46
47 /**
48 * Settings constructor.
49 */
50 public function __construct() {
51 $this->initCustomOptions();
52 $this->hooks();
53 if ( !tpt_fs()->can_use_premium_code() ) {
54 new PremiumSettingsManager();
55 }
56 if ( !tpt_fs()->can_use_premium_code() ) {
57 $template = 'upgrade-banner.php';
58 add_action( 'woocommerce_settings_' . self::SETTINGS_PAGE, function () use($template) {
59 $this->getContainer()->getFileManager()->includeTemplate( 'admin/banners/' . $template, [
60 'upgradeUrl' => tpt_fs_activation_url(),
61 'contactUsUrl' => TierPricingTablePlugin::getContactUsURL(),
62 'documentationUrl' => TierPricingTablePlugin::getDocumentationURL(),
63 ] );
64 } );
65 }
66 if ( tpt_fs()->can_use_premium_code() && !tpt_fs()->is_premium() ) {
67 add_action( 'woocommerce_settings_' . self::SETTINGS_PAGE, function () {
68 $this->getContainer()->getFileManager()->includeTemplate( 'admin/banners/free-version-used-premium-available.php' );
69 } );
70 }
71 }
72
73 protected function initCustomOptions() {
74 $this->getContainer()->add( 'settings.tpt_switch_option', new TPTSwitchOption() );
75 $this->getContainer()->add( 'settings.tpt_integration_option', new TPTIntegrationOption() );
76 $this->getContainer()->add( 'settings.tpt_feature_flag_option', new TPTFeatureFlagOption() );
77 $this->getContainer()->add( 'settings.tpt_text_template', new TPTTextTemplate() );
78 $this->getContainer()->add( 'settings.tpt_display_type', new TPTDisplayType() );
79 $this->getContainer()->add( 'settings.tpt_link_button', new TPTLinkButton() );
80 $this->getContainer()->add( 'settings.tpt_quantity_measurement', new TPTQuantityMeasurementField() );
81 $this->getContainer()->add( 'settings.tpt_multiple_fields', new TPTTableColumnsField() );
82 }
83
84 protected function initSections() {
85 }
86
87 /**
88 * Handle updating settings
89 */
90 public function updateSettings() {
91 woocommerce_update_options( $this->settings );
92 $this->getContainer()->getCache()->purge();
93 }
94
95 /**
96 * Init all settings
97 */
98 public function initSettings() {
99 $this->sections = apply_filters( 'tiered_pricing_table/settings/sections', array(
100 new GeneralSection(),
101 new CalculationLogic(),
102 new AdvancedSection(),
103 new IntegrationSection()
104 ) );
105 foreach ( $this->sections as $section ) {
106 if ( $section->isActive() ) {
107 $this->settings = $section->getSettings();
108 break;
109 }
110 }
111 }
112
113 /**
114 * Register hooks
115 */
116 public function hooks() {
117 add_action( 'init', array($this, 'initSettings') );
118 add_filter( 'woocommerce_settings_tabs_' . self::SETTINGS_PAGE, array($this, 'addTieredPricingTableSettings') );
119 add_filter( 'woocommerce_settings_tabs_array', array($this, 'addSettingsTab'), 50 );
120 add_action( 'woocommerce_update_options_' . self::SETTINGS_PAGE, array($this, 'updateSettings') );
121 add_action( 'woocommerce_settings_' . self::SETTINGS_PAGE, array($this, 'renderSections'), 99 );
122 }
123
124 public function renderSections() {
125 ?>
126 <style>
127 h2 {
128 margin-top: 2em;
129 font-size: 1.45em;
130 }
131 </style>
132
133 <ul class="subsubsub" style="font-size: 1.1em; margin-top: 3px">
134 <?php
135 $sectionsCount = count( $this->sections );
136 ?>
137 <?php
138 foreach ( $this->sections as $index => $section ) {
139 ?>
140 <li>
141 <?php
142 if ( !$section->isActive() ) {
143 ?>
144 <a href="<?php
145 echo esc_attr( $section->getURL() );
146 ?>">
147 <?php
148 if ( $section->isIntegration() ) {
149 ?>
150 <small style="width: 10px; height: 10px; display: inline-block; margin-right: 10px;">
151 <span class="dashicons dashicons-admin-plugins"></span>
152 </small>
153 <b><?php
154 echo esc_html( $section->getName() );
155 ?></b>
156 <?php
157 } else {
158 ?>
159 <?php
160 echo esc_html( $section->getName() );
161 ?>
162 <?php
163 }
164 ?>
165
166 </a>
167 <?php
168 } else {
169 ?>
170
171 <?php
172 if ( $section->getSectionCSS() ) {
173 ?>
174 <style>
175 <?php
176 echo esc_html( $section->getSectionCSS() );
177 ?>
178 </style>
179 <?php
180 }
181 ?>
182
183 <a class="current" href="#">
184 <?php
185 echo esc_html( $section->getName() );
186 ?>
187 </a>
188 <?php
189 }
190 ?>
191 <?php
192 if ( $index < $sectionsCount - 1 ) {
193 ?>
194 |
195 <?php
196 }
197 ?>
198 </li>
199 <?php
200 }
201 ?>
202 </ul>
203 <br class="clear">
204 <?php
205 }
206
207 /**
208 * Add own settings tab
209 *
210 * @param $settingsTabs
211 *
212 * @return array
213 */
214 public static function addSettingsTab( $settingsTabs ) : array {
215 $settingsTabs = ( is_array( $settingsTabs ) ? $settingsTabs : array() );
216 $settingsTabs[self::SETTINGS_PAGE] = __( 'Tiered Pricing', 'tier-pricing-table' );
217 return $settingsTabs;
218 }
219
220 /**
221 * Add settings to WooCommerce
222 */
223 public function addTieredPricingTableSettings() {
224 wp_enqueue_script(
225 'quantity-table-settings-js',
226 $this->getContainer()->getFileManager()->locateJSAsset( 'admin/settings' ),
227 array('jquery'),
228 TierPricingTablePlugin::VERSION
229 );
230 woocommerce_admin_fields( $this->settings );
231 }
232
233 /**
234 * Get setting by name
235 *
236 * @param string $optionName
237 * @param mixed $default
238 *
239 * @return mixed
240 */
241 public function get( string $optionName, $default = null ) {
242 return get_option( self::SETTINGS_PREFIX . $optionName, $default );
243 }
244
245 public static function deleteOptions() {
246 GeneralSection::deleteOptions();
247 AdvancedSection::deleteOptions();
248 IntegrationSection::deleteOptions();
249 }
250
251 /**
252 * Get url to settings page
253 *
254 * @return string
255 */
256 public function getLink() : string {
257 return admin_url( 'admin.php?page=wc-settings&tab=tiered_pricing_table_settings' );
258 }
259
260 public static function hex2rgba( $color, $opacity = false ) : string {
261 $default = 'rgb(0,0,0)';
262 if ( empty( $color ) ) {
263 return $default;
264 }
265 if ( '#' === $color[0] ) {
266 $color = substr( $color, 1 );
267 }
268 if ( strlen( $color ) == 6 ) {
269 $hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
270 } elseif ( strlen( $color ) == 3 ) {
271 $hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
272 } else {
273 return $default;
274 }
275 $rgb = array_map( 'hexdec', $hex );
276 if ( $opacity ) {
277 if ( abs( $opacity ) > 1 ) {
278 $opacity = 1.0;
279 }
280 $output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')';
281 } else {
282 $output = 'rgb(' . implode( ',', $rgb ) . ')';
283 }
284 return $output;
285 }
286
287 public static function shadeHexWithOpacity( $hex, $opacity, $bgHex = '#FFFFFF' ) {
288 // Normalize and parse the foreground hex
289 $hex = ltrim( $hex, '#' );
290 if ( strlen( $hex ) === 3 ) {
291 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
292 }
293 if ( strlen( $hex ) !== 6 ) {
294 return false;
295 }
296 // Normalize and parse the background hex
297 $bgHex = ltrim( $bgHex, '#' );
298 if ( strlen( $bgHex ) === 3 ) {
299 $bgHex = $bgHex[0] . $bgHex[0] . $bgHex[1] . $bgHex[1] . $bgHex[2] . $bgHex[2];
300 }
301 if ( strlen( $bgHex ) !== 6 ) {
302 return false;
303 }
304 // Clamp opacity
305 $opacity = max( 0, min( 1, $opacity ) );
306 // Extract RGB
307 $r = hexdec( substr( $hex, 0, 2 ) );
308 $g = hexdec( substr( $hex, 2, 2 ) );
309 $b = hexdec( substr( $hex, 4, 2 ) );
310 $bgR = hexdec( substr( $bgHex, 0, 2 ) );
311 $bgG = hexdec( substr( $bgHex, 2, 2 ) );
312 $bgB = hexdec( substr( $bgHex, 4, 2 ) );
313 // Blend with background using alpha compositing
314 $newR = round( $opacity * $r + (1 - $opacity) * $bgR );
315 $newG = round( $opacity * $g + (1 - $opacity) * $bgG );
316 $newB = round( $opacity * $b + (1 - $opacity) * $bgB );
317 // Return final hex
318 return sprintf(
319 '#%02X%02X%02X',
320 $newR,
321 $newG,
322 $newB
323 );
324 }
325
326 }
327