| 1 |
<?php namespace TierPricingTable\Settings\Sections\GeneralSection; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\LayoutConfigurator\LayoutConfiguratorAddon; |
| 4 |
use TierPricingTable\Core\ServiceContainer; |
| 5 |
use TierPricingTable\Settings\Sections\GeneralSection\Subsections\CalculationSubsection; |
| 6 |
use TierPricingTable\Settings\Sections\GeneralSection\Subsections\HiddenOptionsSubsection; |
| 7 |
use TierPricingTable\Settings\Sections\GeneralSection\Subsections\LayoutSubsection; |
| 8 |
use TierPricingTable\Settings\Sections\GeneralSection\Subsections\ProductPagePriceSubsection; |
| 9 |
use TierPricingTable\Settings\Sections\SectionAbstract; |
| 10 |
use TierPricingTable\Settings\Settings; |
| 11 |
|
| 12 |
class GeneralSection extends SectionAbstract { |
| 13 |
|
| 14 |
/** |
| 15 |
* Value of the "Layout position" option meaning "I place the layout myself" (shortcode, block, widget). |
| 16 |
* The layout configurator shows it as the automatic display being switched off. |
| 17 |
*/ |
| 18 |
const NONE_POSITION = '____none____'; |
| 19 |
|
| 20 |
const DEFAULT_POSITION_HOOK = 'woocommerce_before_add_to_cart_button'; |
| 21 |
|
| 22 |
public function getSettings() { |
| 23 |
$settings = array(); |
| 24 |
|
| 25 |
foreach ( $this->getSubsections() as $subsection ) { |
| 26 |
$settings = array_merge( $settings, ( new $subsection() )->getWrappedSettings() ); |
| 27 |
} |
| 28 |
|
| 29 |
return apply_filters( 'tiered_pricing_table/settings/general_settings', $settings ); |
| 30 |
} |
| 31 |
|
| 32 |
protected function getSubsections() { |
| 33 |
$subsections = array( |
| 34 |
HiddenOptionsSubsection::class, |
| 35 |
LayoutSubsection::class, |
| 36 |
); |
| 37 |
|
| 38 |
// the layout configurator carries the product page price options while it is on |
| 39 |
if ( ! LayoutConfiguratorAddon::isActive() ) { |
| 40 |
$subsections[] = ProductPagePriceSubsection::class; |
| 41 |
} |
| 42 |
|
| 43 |
$subsections[] = CalculationSubsection::class; |
| 44 |
|
| 45 |
return apply_filters( 'tiered_pricing_table/settings/general_subsections', $subsections ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function deleteOptions() { |
| 49 |
delete_option( Settings::SETTINGS_PREFIX . 'product_page_subsection' ); |
| 50 |
delete_option( Settings::SETTINGS_PREFIX . 'display' ); |
| 51 |
delete_option( Settings::SETTINGS_PREFIX . 'display_type' ); |
| 52 |
delete_option( Settings::SETTINGS_PREFIX . 'quantity_type' ); |
| 53 |
delete_option( Settings::SETTINGS_PREFIX . 'tooltip_color' ); |
| 54 |
delete_option( Settings::SETTINGS_PREFIX . 'tooltip_size' ); |
| 55 |
delete_option( Settings::SETTINGS_PREFIX . 'tooltip_border' ); |
| 56 |
delete_option( Settings::SETTINGS_PREFIX . 'table_title' ); |
| 57 |
delete_option( Settings::SETTINGS_PREFIX . 'position_hook' ); |
| 58 |
delete_option( Settings::SETTINGS_PREFIX . 'selected_quantity_color' ); |
| 59 |
delete_option( Settings::SETTINGS_PREFIX . 'head_quantity_text' ); |
| 60 |
delete_option( Settings::SETTINGS_PREFIX . 'head_price_text' ); |
| 61 |
delete_option( Settings::SETTINGS_PREFIX . 'show_discount_column' ); |
| 62 |
delete_option( Settings::SETTINGS_PREFIX . 'head_discount_text' ); |
| 63 |
delete_option( Settings::SETTINGS_PREFIX . 'table_quantity_measurement' ); |
| 64 |
delete_option( Settings::SETTINGS_PREFIX . 'blocks_quantity_measurement' ); |
| 65 |
delete_option( Settings::SETTINGS_PREFIX . 'product_page_price_format' ); |
| 66 |
delete_option( Settings::SETTINGS_PREFIX . 'clickable_table_rows' ); |
| 67 |
delete_option( Settings::SETTINGS_PREFIX . 'product_page_subsection' ); |
| 68 |
delete_option( Settings::SETTINGS_PREFIX . 'cart_checkout_subsection' ); |
| 69 |
delete_option( Settings::SETTINGS_PREFIX . 'summarize_variations' ); |
| 70 |
delete_option( Settings::SETTINGS_PREFIX . 'show_discount_in_cart' ); |
| 71 |
delete_option( Settings::SETTINGS_PREFIX . 'cart_checkout_subsection' ); |
| 72 |
delete_option( Settings::SETTINGS_PREFIX . 'catalog_prices_subsection' ); |
| 73 |
delete_option( Settings::SETTINGS_PREFIX . 'tiered_price_at_catalog' ); |
| 74 |
delete_option( Settings::SETTINGS_PREFIX . 'tiered_price_at_catalog_for_variable' ); |
| 75 |
delete_option( Settings::SETTINGS_PREFIX . 'tiered_price_at_catalog_cache_for_variable' ); |
| 76 |
delete_option( Settings::SETTINGS_PREFIX . 'tiered_price_at_product_page' ); |
| 77 |
delete_option( Settings::SETTINGS_PREFIX . 'tiered_price_at_catalog_type' ); |
| 78 |
delete_option( Settings::SETTINGS_PREFIX . 'lowest_prefix' ); |
| 79 |
delete_option( Settings::SETTINGS_PREFIX . 'premium_options' ); |
| 80 |
delete_option( Settings::SETTINGS_PREFIX . 'summary_section' ); |
| 81 |
delete_option( Settings::SETTINGS_PREFIX . 'display_summary' ); |
| 82 |
delete_option( Settings::SETTINGS_PREFIX . 'summary_title' ); |
| 83 |
delete_option( Settings::SETTINGS_PREFIX . 'summary_type' ); |
| 84 |
delete_option( Settings::SETTINGS_PREFIX . 'summary_total_label' ); |
| 85 |
delete_option( Settings::SETTINGS_PREFIX . 'summary_each_label' ); |
| 86 |
delete_option( Settings::SETTINGS_PREFIX . 'summary_position_hook' ); |
| 87 |
|
| 88 |
delete_option( Settings::SETTINGS_PREFIX . 'options_option_text' ); |
| 89 |
delete_option( Settings::SETTINGS_PREFIX . 'options_show_default_option' ); |
| 90 |
delete_option( Settings::SETTINGS_PREFIX . 'options_default_option_text' ); |
| 91 |
delete_option( Settings::SETTINGS_PREFIX . 'options_show_original_product_price' ); |
| 92 |
delete_option( Settings::SETTINGS_PREFIX . 'options_show_total' ); |
| 93 |
|
| 94 |
delete_option( Settings::SETTINGS_PREFIX . 'show_total_price' ); |
| 95 |
delete_option( Settings::SETTINGS_PREFIX . 'update_price_on_product_page' ); |
| 96 |
delete_option( Settings::SETTINGS_PREFIX . 'show_tiered_price_as_discount' ); |
| 97 |
} |
| 98 |
|
| 99 |
public function getSlug(): string { |
| 100 |
return 'general'; |
| 101 |
} |
| 102 |
|
| 103 |
public function getName(): string { |
| 104 |
return __( 'General', 'tier-pricing-table' ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Whether the pricing layout is inserted on the product page automatically. Off means the merchant |
| 109 |
* places it with a shortcode, block or widget; the plugin still renders a hidden wrapper so the |
| 110 |
* live price updates keep working. |
| 111 |
*/ |
| 112 |
public static function isAutomaticDisplayEnabled(): bool { |
| 113 |
$settings = ServiceContainer::getInstance()->getSettings(); |
| 114 |
|
| 115 |
if ( self::NONE_POSITION === $settings->get( 'position_hook', self::DEFAULT_POSITION_HOOK ) ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return 'yes' === $settings->get( 'display', 'yes' ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Hook the pricing layout is rendered on. A stored "none" position falls back to the default hook. |
| 124 |
*/ |
| 125 |
public static function getPositionHook(): string { |
| 126 |
$hook = (string) ServiceContainer::getInstance()->getSettings()->get( 'position_hook', self::DEFAULT_POSITION_HOOK ); |
| 127 |
|
| 128 |
return ( '' === $hook || self::NONE_POSITION === $hook ) ? self::DEFAULT_POSITION_HOOK : $hook; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Design style choices per layout. |
| 133 |
*/ |
| 134 |
/** |
| 135 |
* Design styles withheld from shop and category pages, where the layouts sit in narrow grid cards: |
| 136 |
* layout => styles. |
| 137 |
*/ |
| 138 |
public static function getCatalogExcludedStyles(): array { |
| 139 |
return (array) apply_filters( 'tiered_pricing_table/catalog/excluded_styles', array( |
| 140 |
'blocks' => array( 'style-8' ), |
| 141 |
) ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Design styles per layout. With $catalog, the styles withheld from shop and category pages are left out. |
| 146 |
*/ |
| 147 |
public static function getStyleOptions( bool $catalog = false ): array { |
| 148 |
$styles = function ( int $count ) { |
| 149 |
$options = array( 'default' => __( 'Default', 'tier-pricing-table' ) ); |
| 150 |
for ( $i = 1; $i <= $count; $i ++ ) { |
| 151 |
/* translators: %d: style number */ |
| 152 |
$options[ 'style-' . $i ] = sprintf( __( 'Style #%d', 'tier-pricing-table' ), $i ); |
| 153 |
} |
| 154 |
|
| 155 |
return $options; |
| 156 |
}; |
| 157 |
|
| 158 |
$options = array( |
| 159 |
'table' => $styles( 6 ), |
| 160 |
'blocks' => $styles( 8 ), |
| 161 |
'options' => $styles( 6 ), |
| 162 |
'dropdown' => $styles( 1 ), |
| 163 |
'plain-text' => $styles( 2 ), |
| 164 |
); |
| 165 |
|
| 166 |
if ( $catalog ) { |
| 167 |
foreach ( self::getCatalogExcludedStyles() as $layout => $excluded ) { |
| 168 |
if ( isset( $options[ $layout ] ) ) { |
| 169 |
$options[ $layout ] = array_diff_key( $options[ $layout ], array_flip( (array) $excluded ) ); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $options; |
| 175 |
} |
| 176 |
|
| 177 |
public static function getOptionText() { |
| 178 |
$default = __( '<strong>Buy {tp_quantity} pieces and save {tp_rounded_discount}%</strong>', |
| 179 |
'tier-pricing-table' ); |
| 180 |
|
| 181 |
return ServiceContainer::getInstance()->getSettings()->get( 'options_option_text', $default ); |
| 182 |
} |
| 183 |
|
| 184 |
public static function getPlainTextTemplate() { |
| 185 |
$default = __( '<strong>Buy {tp_quantity} pieces for {tp_price} each and save {tp_rounded_discount}%</strong>', |
| 186 |
'tier-pricing-table' ); |
| 187 |
|
| 188 |
return ServiceContainer::getInstance()->getSettings()->get( 'plain_text_template', $default ); |
| 189 |
} |
| 190 |
|
| 191 |
public static function getPlainTextFirstTierTemplate() { |
| 192 |
$default = __( '<strong>Buy {tp_quantity} pieces for {tp_price}</strong>', 'tier-pricing-table' ); |
| 193 |
|
| 194 |
return ServiceContainer::getInstance()->getSettings()->get( 'plain_text_first_tier_template', $default ); |
| 195 |
} |
| 196 |
|
| 197 |
public static function isPlainTextFirstTierEnabled(): bool { |
| 198 |
return ServiceContainer::getInstance()->getSettings()->get( 'plain_text_show_first_tier', 'yes' ) === 'yes'; |
| 199 |
} |
| 200 |
|
| 201 |
public static function getDefaultOptionText() { |
| 202 |
$default = __( '<strong>Buy {tp_quantity} pieces</strong>', 'tier-pricing-table' ); |
| 203 |
|
| 204 |
return ServiceContainer::getInstance()->getSettings()->get( 'options_default_option_text', $default ); |
| 205 |
} |
| 206 |
|
| 207 |
public static function isDefaultOptionEnabled(): bool { |
| 208 |
return ServiceContainer::getInstance()->getSettings()->get( 'options_show_default_option', 'yes' ) === 'yes'; |
| 209 |
} |
| 210 |
|
| 211 |
public static function isShowOriginalProductPrice(): bool { |
| 212 |
return ServiceContainer::getInstance()->getSettings()->get( 'options_show_original_product_price', |
| 213 |
'yes' ) === 'yes'; |
| 214 |
} |
| 215 |
|
| 216 |
public static function isShowOptionTotal(): bool { |
| 217 |
return ServiceContainer::getInstance()->getSettings()->get( 'options_show_total', 'yes' ) === 'yes'; |
| 218 |
} |
| 219 |
|
| 220 |
public static function getPricingBlocksStyle(): string { |
| 221 |
return ServiceContainer::getInstance()->getSettings()->get( 'pricing_blocks_style', 'default' ); |
| 222 |
} |
| 223 |
|
| 224 |
public static function getPricingOptionsStyle(): string { |
| 225 |
return ServiceContainer::getInstance()->getSettings()->get( 'pricing_options_style', 'default' ); |
| 226 |
} |
| 227 |
|
| 228 |
public static function getPricingTableStyle(): string { |
| 229 |
return ServiceContainer::getInstance()->getSettings()->get( 'pricing_table_style', 'default' ); |
| 230 |
} |
| 231 |
|
| 232 |
public static function getPricingDropdownStyle(): string { |
| 233 |
return ServiceContainer::getInstance()->getSettings()->get( 'pricing_dropdown_style', 'default' ); |
| 234 |
} |
| 235 |
|
| 236 |
public static function getPricingPlainTextStyle(): string { |
| 237 |
return ServiceContainer::getInstance()->getSettings()->get( 'pricing_plain_text_style', 'default' ); |
| 238 |
} |
| 239 |
} |
| 240 |
|