| 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 |
add_action( 'admin_enqueue_scripts', function () { |
| 72 |
$assetName = self::SETTINGS_PAGE . '_script'; |
| 73 |
wp_register_script( |
| 74 |
$assetName, |
| 75 |
$this->getContainer()->getFileManager()->locateJSAsset( 'admin/settings' ), |
| 76 |
array('jquery'), |
| 77 |
TierPricingTablePlugin::VERSION, |
| 78 |
true |
| 79 |
); |
| 80 |
wp_enqueue_script( $assetName ); |
| 81 |
} ); |
| 82 |
} |
| 83 |
|
| 84 |
protected function initCustomOptions() { |
| 85 |
$this->getContainer()->add( 'settings.tpt_switch_option', new TPTSwitchOption() ); |
| 86 |
$this->getContainer()->add( 'settings.tpt_integration_option', new TPTIntegrationOption() ); |
| 87 |
$this->getContainer()->add( 'settings.tpt_feature_flag_option', new TPTFeatureFlagOption() ); |
| 88 |
$this->getContainer()->add( 'settings.tpt_text_template', new TPTTextTemplate() ); |
| 89 |
$this->getContainer()->add( 'settings.tpt_display_type', new TPTDisplayType() ); |
| 90 |
$this->getContainer()->add( 'settings.tpt_link_button', new TPTLinkButton() ); |
| 91 |
$this->getContainer()->add( 'settings.tpt_quantity_measurement', new TPTQuantityMeasurementField() ); |
| 92 |
$this->getContainer()->add( 'settings.tpt_multiple_fields', new TPTTableColumnsField() ); |
| 93 |
} |
| 94 |
|
| 95 |
protected function initSections() { |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Handle updating settings |
| 100 |
*/ |
| 101 |
public function updateSettings() { |
| 102 |
woocommerce_update_options( $this->settings ); |
| 103 |
$this->getContainer()->getCache()->purge(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Init all settings |
| 108 |
*/ |
| 109 |
public function initSettings() { |
| 110 |
$this->sections = apply_filters( 'tiered_pricing_table/settings/sections', array( |
| 111 |
new GeneralSection(), |
| 112 |
new CalculationLogic(), |
| 113 |
new AdvancedSection(), |
| 114 |
new IntegrationSection() |
| 115 |
) ); |
| 116 |
foreach ( $this->sections as $section ) { |
| 117 |
if ( $section->isActive() ) { |
| 118 |
$this->settings = $section->getSettings(); |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Register hooks |
| 126 |
*/ |
| 127 |
public function hooks() { |
| 128 |
add_action( 'init', array($this, 'initSettings') ); |
| 129 |
add_filter( 'woocommerce_settings_tabs_' . self::SETTINGS_PAGE, array($this, 'addTieredPricingTableSettings') ); |
| 130 |
add_filter( 'woocommerce_settings_tabs_array', array($this, 'addSettingsTab'), 50 ); |
| 131 |
add_action( 'woocommerce_update_options_' . self::SETTINGS_PAGE, array($this, 'updateSettings') ); |
| 132 |
add_action( 'woocommerce_settings_' . self::SETTINGS_PAGE, array($this, 'renderSections'), 99 ); |
| 133 |
} |
| 134 |
|
| 135 |
public function renderSections() { |
| 136 |
?> |
| 137 |
<style> |
| 138 |
h2 { |
| 139 |
margin-top: 2em; |
| 140 |
font-size: 1.45em; |
| 141 |
} |
| 142 |
</style> |
| 143 |
|
| 144 |
<ul class="subsubsub" style="font-size: 1.1em; margin-top: 3px"> |
| 145 |
<?php |
| 146 |
$sectionsCount = count( $this->sections ); |
| 147 |
?> |
| 148 |
<?php |
| 149 |
foreach ( $this->sections as $index => $section ) { |
| 150 |
?> |
| 151 |
<li> |
| 152 |
<?php |
| 153 |
if ( !$section->isActive() ) { |
| 154 |
?> |
| 155 |
<a href="<?php |
| 156 |
echo esc_attr( $section->getURL() ); |
| 157 |
?>"> |
| 158 |
<?php |
| 159 |
if ( $section->isIntegration() ) { |
| 160 |
?> |
| 161 |
<small style="width: 10px; height: 10px; display: inline-block; margin-right: 10px;"> |
| 162 |
<span class="dashicons dashicons-admin-plugins"></span> |
| 163 |
</small> |
| 164 |
<b><?php |
| 165 |
echo esc_html( $section->getName() ); |
| 166 |
?></b> |
| 167 |
<?php |
| 168 |
} else { |
| 169 |
?> |
| 170 |
<?php |
| 171 |
echo esc_html( $section->getName() ); |
| 172 |
?> |
| 173 |
<?php |
| 174 |
} |
| 175 |
?> |
| 176 |
|
| 177 |
</a> |
| 178 |
<?php |
| 179 |
} else { |
| 180 |
?> |
| 181 |
|
| 182 |
<?php |
| 183 |
if ( $section->getSectionCSS() ) { |
| 184 |
?> |
| 185 |
<style> |
| 186 |
<?php |
| 187 |
echo esc_html( $section->getSectionCSS() ); |
| 188 |
?> |
| 189 |
</style> |
| 190 |
<?php |
| 191 |
} |
| 192 |
?> |
| 193 |
|
| 194 |
<a class="current" href="#"> |
| 195 |
<?php |
| 196 |
echo esc_html( $section->getName() ); |
| 197 |
?> |
| 198 |
</a> |
| 199 |
<?php |
| 200 |
} |
| 201 |
?> |
| 202 |
<?php |
| 203 |
if ( $index < $sectionsCount - 1 ) { |
| 204 |
?> |
| 205 |
| |
| 206 |
<?php |
| 207 |
} |
| 208 |
?> |
| 209 |
</li> |
| 210 |
<?php |
| 211 |
} |
| 212 |
?> |
| 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 |
woocommerce_admin_fields( $this->settings ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Get setting by name |
| 240 |
* |
| 241 |
* @param string $optionName |
| 242 |
* @param mixed $default |
| 243 |
* |
| 244 |
* @return mixed |
| 245 |
*/ |
| 246 |
public function get( string $optionName, $default = null ) { |
| 247 |
return get_option( self::SETTINGS_PREFIX . $optionName, $default ); |
| 248 |
} |
| 249 |
|
| 250 |
public static function deleteOptions() { |
| 251 |
GeneralSection::deleteOptions(); |
| 252 |
AdvancedSection::deleteOptions(); |
| 253 |
IntegrationSection::deleteOptions(); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Get url to settings page |
| 258 |
* |
| 259 |
* @return string |
| 260 |
*/ |
| 261 |
public function getLink() : string { |
| 262 |
return admin_url( 'admin.php?page=wc-settings&tab=tiered_pricing_table_settings' ); |
| 263 |
} |
| 264 |
|
| 265 |
public static function hex2rgba( $color, $opacity = false ) : string { |
| 266 |
$default = 'rgb(0,0,0)'; |
| 267 |
if ( empty( $color ) ) { |
| 268 |
return $default; |
| 269 |
} |
| 270 |
if ( '#' === $color[0] ) { |
| 271 |
$color = substr( $color, 1 ); |
| 272 |
} |
| 273 |
if ( strlen( $color ) == 6 ) { |
| 274 |
$hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); |
| 275 |
} elseif ( strlen( $color ) == 3 ) { |
| 276 |
$hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); |
| 277 |
} else { |
| 278 |
return $default; |
| 279 |
} |
| 280 |
$rgb = array_map( 'hexdec', $hex ); |
| 281 |
if ( $opacity ) { |
| 282 |
if ( abs( $opacity ) > 1 ) { |
| 283 |
$opacity = 1.0; |
| 284 |
} |
| 285 |
$output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')'; |
| 286 |
} else { |
| 287 |
$output = 'rgb(' . implode( ',', $rgb ) . ')'; |
| 288 |
} |
| 289 |
return $output; |
| 290 |
} |
| 291 |
|
| 292 |
public static function shadeHexWithOpacity( $hex, $opacity, $bgHex = '#FFFFFF' ) { |
| 293 |
// Normalize and parse the foreground hex |
| 294 |
$hex = ltrim( $hex, '#' ); |
| 295 |
if ( strlen( $hex ) === 3 ) { |
| 296 |
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 297 |
} |
| 298 |
if ( strlen( $hex ) !== 6 ) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
// Normalize and parse the background hex |
| 302 |
$bgHex = ltrim( $bgHex, '#' ); |
| 303 |
if ( strlen( $bgHex ) === 3 ) { |
| 304 |
$bgHex = $bgHex[0] . $bgHex[0] . $bgHex[1] . $bgHex[1] . $bgHex[2] . $bgHex[2]; |
| 305 |
} |
| 306 |
if ( strlen( $bgHex ) !== 6 ) { |
| 307 |
return false; |
| 308 |
} |
| 309 |
// Clamp opacity |
| 310 |
$opacity = max( 0, min( 1, $opacity ) ); |
| 311 |
// Extract RGB |
| 312 |
$r = hexdec( substr( $hex, 0, 2 ) ); |
| 313 |
$g = hexdec( substr( $hex, 2, 2 ) ); |
| 314 |
$b = hexdec( substr( $hex, 4, 2 ) ); |
| 315 |
$bgR = hexdec( substr( $bgHex, 0, 2 ) ); |
| 316 |
$bgG = hexdec( substr( $bgHex, 2, 2 ) ); |
| 317 |
$bgB = hexdec( substr( $bgHex, 4, 2 ) ); |
| 318 |
// Blend with background using alpha compositing |
| 319 |
$newR = round( $opacity * $r + (1 - $opacity) * $bgR ); |
| 320 |
$newG = round( $opacity * $g + (1 - $opacity) * $bgG ); |
| 321 |
$newB = round( $opacity * $b + (1 - $opacity) * $bgB ); |
| 322 |
// Return final hex |
| 323 |
return sprintf( |
| 324 |
'#%02X%02X%02X', |
| 325 |
$newR, |
| 326 |
$newG, |
| 327 |
$newB |
| 328 |
); |
| 329 |
} |
| 330 |
|
| 331 |
} |
| 332 |
|