← All changes
|
src/Settings/Sections/GeneralSection/GeneralSection.php
+99
-3
7.1.1
→
8.0.2
View file →
| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | <?php namespace TierPricingTable\Settings\Sections\GeneralSection; |
| 2 | 2 | |
| 3 | +use TierPricingTable\Addons\LayoutConfigurator\LayoutConfiguratorAddon; | |
| 3 | 4 | use TierPricingTable\Core\ServiceContainer; |
| 5 | +use TierPricingTable\Settings\Sections\GeneralSection\Subsections\CalculationSubsection; | |
| 4 | 6 | use TierPricingTable\Settings\Sections\GeneralSection\Subsections\HiddenOptionsSubsection; |
| 5 | 7 | use TierPricingTable\Settings\Sections\GeneralSection\Subsections\LayoutSubsection; |
| 6 | 8 | use TierPricingTable\Settings\Sections\GeneralSection\Subsections\ProductPagePriceSubsection; |
| 7 | 9 | use TierPricingTable\Settings\Sections\SectionAbstract; |
| @@ -8,8 +10,16 @@ | ||
| 8 | 10 | use TierPricingTable\Settings\Settings; |
| 9 | 11 | |
| 10 | 12 | class GeneralSection extends SectionAbstract { |
| 11 | 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 | + | |
| 12 | 22 | public function getSettings() { |
| 13 | 23 | $settings = array(); |
| 14 | 24 | |
| 15 | 25 | foreach ( $this->getSubsections() as $subsection ) { |
| @@ -19,13 +29,21 @@ | ||
| 19 | 29 | return apply_filters( 'tiered_pricing_table/settings/general_settings', $settings ); |
| 20 | 30 | } |
| 21 | 31 | |
| 22 | 32 | protected function getSubsections() { |
| 23 | - return apply_filters( 'tiered_pricing_table/settings/general_subsections', array( | |
| 33 | + $subsections = array( | |
| 24 | 34 | HiddenOptionsSubsection::class, |
| 25 | 35 | LayoutSubsection::class, |
| 26 | - ProductPagePriceSubsection::class, | |
| 27 | - ) ); | |
| 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 ); | |
| 28 | 46 | } |
| 29 | 47 | |
| 30 | 48 | public static function deleteOptions() { |
| 31 | 49 | delete_option( Settings::SETTINGS_PREFIX . 'product_page_subsection' ); |
| @@ -85,8 +103,78 @@ | ||
| 85 | 103 | public function getName(): string { |
| 86 | 104 | return __( 'General', 'tier-pricing-table' ); |
| 87 | 105 | } |
| 88 | 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 | + | |
| 89 | 177 | public static function getOptionText() { |
| 90 | 178 | $default = __( '<strong>Buy {tp_quantity} pieces and save {tp_rounded_discount}%</strong>', |
| 91 | 179 | 'tier-pricing-table' ); |
| 92 | 180 | |
| @@ -138,6 +226,14 @@ | ||
| 138 | 226 | } |
| 139 | 227 | |
| 140 | 228 | public static function getPricingTableStyle(): string { |
| 141 | 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' ); | |
| 142 | 238 | } |
| 143 | 239 | } |