PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.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.1.0, at src/Settings/Settings.php

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