| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | <?php namespace TierPricingTable\Settings\CustomOptions; |
| 2 | 2 | |
| 3 | 3 | use TierPricingTable\Core\ServiceContainerTrait; |
| 4 | +use TierPricingTable\Settings\Settings; | |
| 4 | 5 | |
| 5 | 6 | class TPTTextTemplate { |
| 6 | - | |
| 7 | + | |
| 7 | 8 | const FIELD_TYPE = 'tpt_text_template'; |
| 8 | - | |
| 9 | + | |
| 9 | 10 | /** |
| 10 | 11 | * Editor id |
| 11 | 12 | * |
| 12 | 13 | * @var mixed|string |
| @@ -11,50 +12,51 @@ | ||
| 11 | 12 | * |
| 12 | 13 | * @var mixed|string |
| 13 | 14 | */ |
| 14 | 15 | private $editorId; |
| 15 | - | |
| 16 | + | |
| 16 | 17 | use ServiceContainerTrait; |
| 17 | - | |
| 18 | + | |
| 18 | 19 | public function __construct() { |
| 19 | 20 | add_action( 'woocommerce_admin_field_' . self::FIELD_TYPE, array( $this, 'render' ) ); |
| 20 | - | |
| 21 | - add_action( 'admin_head', function () { | |
| 22 | - ?> | |
| 23 | - <script> | |
| 24 | - var TPTAvailableCustomButtons = JSON.parse('<?php echo wp_kses_post( wp_json_encode( $this->getAvailableVariables() ) ); ?>'); | |
| 25 | - </script> | |
| 26 | - | |
| 27 | - <?php | |
| 28 | - } ); | |
| 29 | - | |
| 21 | + | |
| 22 | + add_action( 'admin_enqueue_scripts', function () { | |
| 23 | + $assetName = Settings::SETTINGS_PAGE . '_script'; | |
| 24 | + | |
| 25 | + wp_add_inline_script( $assetName, | |
| 26 | + 'const TPTAvailableCustomButtons = ' . wp_json_encode( $this->getAvailableVariables() ) . ';', | |
| 27 | + 'before' ); | |
| 28 | + }, 999 ); | |
| 29 | + | |
| 30 | 30 | add_filter( 'mce_external_plugins', function ( $plugins, $editor ) { |
| 31 | 31 | if ( $this->editorId === $editor ) { |
| 32 | 32 | $plugins['tiered-pricing-custom-mce-buttons'] = $this->getContainer()->getFileManager()->locateJSAsset( 'admin/mce' ); |
| 33 | 33 | } |
| 34 | - | |
| 34 | + | |
| 35 | 35 | return $plugins; |
| 36 | 36 | }, 10, 2 ); |
| 37 | - | |
| 37 | + | |
| 38 | 38 | add_filter( 'mce_buttons', function ( $buttons ) { |
| 39 | - | |
| 39 | + | |
| 40 | 40 | $buttons = array_merge( $buttons, array_keys( $this->getAvailableVariables() ) ); |
| 41 | - | |
| 41 | + | |
| 42 | 42 | return $buttons; |
| 43 | 43 | } ); |
| 44 | - | |
| 44 | + | |
| 45 | 45 | add_action( 'woocommerce_admin_settings_sanitize_option', function ( $value, $option, $rawValue ) { |
| 46 | - | |
| 46 | + | |
| 47 | 47 | if ( self::FIELD_TYPE === $option['type'] ) { |
| 48 | 48 | return wp_kses_post( $rawValue ); |
| 49 | 49 | } |
| 50 | - | |
| 50 | + | |
| 51 | 51 | return $value; |
| 52 | 52 | }, 10, 3 ); |
| 53 | - | |
| 53 | + | |
| 54 | 54 | } |
| 55 | - | |
| 55 | + | |
| 56 | 56 | public function render( $value ) { |
| 57 | + // the description is plugin-defined text (settings arrays), read once for output | |
| 58 | + $description = (string) ( $value['desc'] ?? '' ); | |
| 57 | 59 | if ( ! isset( $value['id'] ) ) { |
| 58 | 60 | $value['id'] = ''; |
| 59 | 61 | } |
| 60 | 62 | if ( ! isset( $value['title'] ) ) { |
| @@ -74,13 +76,13 @@ | ||
| 74 | 76 | } |
| 75 | 77 | if ( ! isset( $value['value'] ) ) { |
| 76 | 78 | $value['value'] = \WC_Admin_Settings::get_option( $value['id'], $value['default'] ); |
| 77 | 79 | } |
| 78 | - | |
| 80 | + | |
| 79 | 81 | $option_value = $value['value']; |
| 80 | - | |
| 82 | + | |
| 81 | 83 | $this->editorId = $value['id']; |
| 82 | - | |
| 84 | + | |
| 83 | 85 | $value['placeholders'] = is_array( $value['placeholders'] ) ? $value['placeholders'] : array(); |
| 84 | 86 | ?> |
| 85 | 87 | <style> |
| 86 | 88 | #wp-<?php echo esc_html($value['id']); ?>-wrap .wp-editor-tools { |
| @@ -97,116 +99,122 @@ | ||
| 97 | 99 | </th> |
| 98 | 100 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 99 | 101 | <?php |
| 100 | 102 | wp_editor( $option_value, $value['id'], array( |
| 101 | - 'wpautop' => true, | |
| 102 | - 'media_buttons' => false, | |
| 103 | - | |
| 104 | - 'textarea_name' => $value['id'], | |
| 105 | - 'editor_height' => 30, | |
| 106 | - 'tabindex' => null, | |
| 107 | - 'editor_class' => 'tpt-message-template-mce', | |
| 108 | - 'tinymce' => array( | |
| 109 | - 'resize' => 'vertical', | |
| 110 | - 'menubar' => false, | |
| 111 | - 'wpautop' => true, | |
| 112 | - 'toolbar2' => '', | |
| 113 | - 'toolbar1' => implode( ',', array_merge( array( | |
| 114 | - 'bold', | |
| 115 | - 'italic', | |
| 116 | - 'strikethrough', | |
| 117 | - 'link', | |
| 118 | - | |
| 119 | - 'spellchecker', | |
| 120 | - ), $value['placeholders'] ) ), | |
| 121 | - ), | |
| 122 | - 'quicktags' => array( | |
| 123 | - 'id' => $value['id'], | |
| 124 | - 'buttons' => 'strong,em,del', | |
| 125 | - ), | |
| 126 | - 'drag_drop_upload' => false, | |
| 103 | + 'wpautop' => true, | |
| 104 | + 'media_buttons' => false, | |
| 105 | + | |
| 106 | + 'textarea_name' => $value['id'], | |
| 107 | + 'editor_height' => 30, | |
| 108 | + 'tabindex' => null, | |
| 109 | + 'editor_class' => 'tpt-message-template-mce', | |
| 110 | + 'tinymce' => array( | |
| 111 | + 'resize' => 'vertical', | |
| 112 | + 'menubar' => false, | |
| 113 | + 'wpautop' => true, | |
| 114 | + 'toolbar2' => '', | |
| 115 | + 'toolbar1' => implode( ',', array_merge( array( | |
| 116 | + 'bold', | |
| 117 | + 'italic', | |
| 118 | + 'strikethrough', | |
| 119 | + 'link', | |
| 120 | + | |
| 121 | + 'spellchecker', | |
| 122 | + ), $value['placeholders'] ) ), | |
| 123 | + ), | |
| 124 | + 'quicktags' => array( | |
| 125 | + 'id' => $value['id'], | |
| 126 | + 'buttons' => 'strong,em,del', | |
| 127 | + ), | |
| 128 | + 'drag_drop_upload' => false, | |
| 127 | 129 | ) ); |
| 128 | 130 | ?> |
| 129 | - | |
| 131 | + | |
| 130 | 132 | <?php if ( $value['desc'] ) : ?> |
| 131 | - <p class="description"><?php echo wp_kses_post( $value['desc'] ); ?></p> | |
| 133 | + <p class="description"><?php echo wp_kses_post( $description ); // nosemgrep ?></p> | |
| 132 | 134 | <?php endif; ?> |
| 133 | 135 | </td> |
| 134 | 136 | </tr> |
| 135 | 137 | <?php |
| 136 | 138 | } |
| 137 | - | |
| 139 | + | |
| 138 | 140 | protected function getAvailableVariables() { |
| 139 | - return array( | |
| 140 | - 'tp_quantity' => array( | |
| 141 | - 'name' => __( 'Quantity', 'tier-pricing-table' ), | |
| 142 | - 'description' => __( '{tp_quantity} - range or a static quantity of the current pricing tier.', | |
| 143 | - 'tier-pricing-table' ), | |
| 144 | - 'variableKey' => '{tp_quantity}', | |
| 145 | - ), | |
| 146 | - | |
| 147 | - 'tp_discount' => array( | |
| 148 | - 'name' => __( 'Percentage discount', 'tier-pricing-table' ), | |
| 149 | - 'description' => __( '{tp_discount} - current tier`s percentage discount.', 'tier-pricing-table' ), | |
| 150 | - 'variableKey' => '{tp_discount}', | |
| 151 | - ), | |
| 152 | - | |
| 153 | - 'tp_rounded_discount' => array( | |
| 154 | - 'name' => __( 'Rounded percentage discount', 'tier-pricing-table' ), | |
| 155 | - 'description' => __( '{tp_rounded_discount} - current tier`s rounded percentage discount.', | |
| 156 | - 'tier-pricing-table' ), | |
| 157 | - 'variableKey' => '{tp_rounded_discount}', | |
| 158 | - ), | |
| 159 | - | |
| 160 | - 'tp_required_quantity' => array( | |
| 161 | - 'name' => __( 'Required quantity', 'tier-pricing-table' ), | |
| 162 | - 'description' => __( '{tp_required_quantity} - required quantity to add a next tiered pricing.', | |
| 163 | - 'tier-pricing-table' ), | |
| 164 | - 'variableKey' => '{tp_required_quantity}', | |
| 165 | - ), | |
| 166 | - | |
| 167 | - 'tp_next_price' => array( | |
| 168 | - 'name' => __( 'Next price', 'tier-pricing-table' ), | |
| 169 | - 'description' => __( '{tp_next_price} - next tiered pricing price user can get if they add required quantity.', | |
| 170 | - 'tier-pricing-table' ), | |
| 171 | - 'variableKey' => '{tp_next_price}', | |
| 172 | - ), | |
| 173 | - | |
| 174 | - 'tp_next_discount' => array( | |
| 175 | - 'name' => __( 'Next discount', 'tier-pricing-table' ), | |
| 176 | - 'description' => __( '{tp_next_discount} - next discount if they add required quantity.', | |
| 177 | - 'tier-pricing-table' ), | |
| 178 | - 'variableKey' => '{tp_next_discount}', | |
| 179 | - ), | |
| 180 | - | |
| 181 | - 'tp_actual_discount' => array( | |
| 182 | - 'name' => __( 'Next actual discount', 'tier-pricing-table' ), | |
| 183 | - 'description' => __( '{tp_actual_discount} - discount based on the original product price.', | |
| 184 | - 'tier-pricing-table' ), | |
| 185 | - 'variableKey' => '{tp_actual_discount}', | |
| 186 | - ), | |
| 187 | - 'tp_ys_price' => array( | |
| 188 | - 'name' => __( 'Save price', 'tier-pricing-table' ), | |
| 189 | - 'description' => __( '{tp_ys_price} - difference between regular and current price.', | |
| 190 | - 'tier-pricing-table' ), | |
| 191 | - 'variableKey' => '{tp_ys_price}', | |
| 192 | - ), | |
| 193 | - 'tp_ys_total_price' => array( | |
| 194 | - 'name' => __( 'Total save price', 'tier-pricing-table' ), | |
| 195 | - 'description' => __( '{tp_ys_total_price} - difference between regular and current price multiplied by quantity.', | |
| 196 | - 'tier-pricing-table' ), | |
| 197 | - 'variableKey' => '{tp_ys_total_price}', | |
| 198 | - ), | |
| 199 | - 'tp_price' => array( | |
| 200 | - 'name' => __( 'Price', 'tier-pricing-table' ), | |
| 201 | - 'description' => __( '{tp_price} - Tier price for one piece', 'tier-pricing-table' ), | |
| 202 | - 'variableKey' => '{tp_price}', | |
| 203 | - ), | |
| 204 | - 'tp_base_unit_name' => array( | |
| 205 | - 'name' => __( 'Unit label', 'tier-pricing-table' ), | |
| 206 | - 'description' => __( 'It will use Unit label from products if you set it.', 'tier-pricing-table' ), | |
| 207 | - 'variableKey' => '{tp_base_unit_name}', | |
| 208 | - ), | |
| 209 | - | |
| 210 | - ); | |
| 141 | + return apply_filters( 'tiered_pricing_table/text_template_variables', array( | |
| 142 | + 'tp_quantity' => array( | |
| 143 | + 'name' => __( 'Quantity', 'tier-pricing-table' ), | |
| 144 | + 'description' => __( '{tp_quantity} - range or a static quantity of the current pricing tier.', | |
| 145 | + 'tier-pricing-table' ), | |
| 146 | + 'variableKey' => '{tp_quantity}', | |
| 147 | + ), | |
| 148 | + | |
| 149 | + 'tp_discount' => array( | |
| 150 | + 'name' => __( 'Percentage discount', 'tier-pricing-table' ), | |
| 151 | + 'description' => __( '{tp_discount} - current tier`s percentage discount.', | |
| 152 | + 'tier-pricing-table' ), | |
| 153 | + 'variableKey' => '{tp_discount}', | |
| 154 | + ), | |
| 155 | + | |
| 156 | + 'tp_rounded_discount' => array( | |
| 157 | + 'name' => __( 'Rounded percentage discount', 'tier-pricing-table' ), | |
| 158 | + 'description' => __( '{tp_rounded_discount} - current tier`s rounded percentage discount.', | |
| 159 | + 'tier-pricing-table' ), | |
| 160 | + 'variableKey' => '{tp_rounded_discount}', | |
| 161 | + ), | |
| 162 | + | |
| 163 | + 'tp_required_quantity' => array( | |
| 164 | + 'name' => __( 'Required quantity', 'tier-pricing-table' ), | |
| 165 | + 'description' => __( '{tp_required_quantity} - required quantity to add a next tiered pricing.', | |
| 166 | + 'tier-pricing-table' ), | |
| 167 | + 'variableKey' => '{tp_required_quantity}', | |
| 168 | + ), | |
| 169 | + | |
| 170 | + 'tp_next_price' => array( | |
| 171 | + 'name' => __( 'Next price', 'tier-pricing-table' ), | |
| 172 | + 'description' => __( '{tp_next_price} - next tiered pricing price user can get if they add required quantity.', | |
| 173 | + 'tier-pricing-table' ), | |
| 174 | + 'variableKey' => '{tp_next_price}', | |
| 175 | + ), | |
| 176 | + | |
| 177 | + 'tp_next_discount' => array( | |
| 178 | + 'name' => __( 'Next discount', 'tier-pricing-table' ), | |
| 179 | + 'description' => __( '{tp_next_discount} - next discount if they add required quantity.', | |
| 180 | + 'tier-pricing-table' ), | |
| 181 | + 'variableKey' => '{tp_next_discount}', | |
| 182 | + ), | |
| 183 | + | |
| 184 | + 'tp_actual_discount' => array( | |
| 185 | + 'name' => __( 'Next actual discount', 'tier-pricing-table' ), | |
| 186 | + 'description' => __( '{tp_actual_discount} - discount based on the original product price.', | |
| 187 | + 'tier-pricing-table' ), | |
| 188 | + 'variableKey' => '{tp_actual_discount}', | |
| 189 | + ), | |
| 190 | + 'tp_ys_price' => array( | |
| 191 | + 'name' => __( 'Save price', 'tier-pricing-table' ), | |
| 192 | + 'description' => __( '{tp_ys_price} - difference between regular and current price.', | |
| 193 | + 'tier-pricing-table' ), | |
| 194 | + 'variableKey' => '{tp_ys_price}', | |
| 195 | + ), | |
| 196 | + 'tp_ys_total_price' => array( | |
| 197 | + 'name' => __( 'Total save price', 'tier-pricing-table' ), | |
| 198 | + 'description' => __( '{tp_ys_total_price} - difference between regular and current price multiplied by quantity.', | |
| 199 | + 'tier-pricing-table' ), | |
| 200 | + 'variableKey' => '{tp_ys_total_price}', | |
| 201 | + ), | |
| 202 | + 'tp_ys_percentage_discount' => array( | |
| 203 | + 'name' => __( 'Percentage discount', 'tier-pricing-table' ), | |
| 204 | + 'variableKey' => '{tp_ys_percentage_discount}', | |
| 205 | + ), | |
| 206 | + 'tp_price' => array( | |
| 207 | + 'name' => __( 'Price', 'tier-pricing-table' ), | |
| 208 | + 'description' => __( '{tp_price} - Tier price for one piece', 'tier-pricing-table' ), | |
| 209 | + 'variableKey' => '{tp_price}', | |
| 210 | + ), | |
| 211 | + 'tp_base_unit_name' => array( | |
| 212 | + 'name' => __( 'Unit label', 'tier-pricing-table' ), | |
| 213 | + 'description' => __( 'It will use Unit label from products if you set it.', | |
| 214 | + 'tier-pricing-table' ), | |
| 215 | + 'variableKey' => '{tp_base_unit_name}', | |
| 216 | + ), | |
| 217 | + | |
| 218 | + ) ); | |
| 211 | 219 | } |
| 212 | 220 | } |