PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
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 / CustomOptions / TPTTextTemplate.php

TPTTextTemplate.php in Tiered Pricing Table for WooCommerce 8.0.2, at src/Settings/CustomOptions/TPTTextTemplate.php

221 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Settings\CustomOptions;
2
3 use TierPricingTable\Core\ServiceContainerTrait;
4 use TierPricingTable\Settings\Settings;
5
6 class TPTTextTemplate {
7
8 const FIELD_TYPE = 'tpt_text_template';
9
10 /**
11 * Editor id
12 *
13 * @var mixed|string
14 */
15 private $editorId;
16
17 use ServiceContainerTrait;
18
19 public function __construct() {
20 add_action( 'woocommerce_admin_field_' . self::FIELD_TYPE, array( $this, 'render' ) );
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 add_filter( 'mce_external_plugins', function ( $plugins, $editor ) {
31 if ( $this->editorId === $editor ) {
32 $plugins['tiered-pricing-custom-mce-buttons'] = $this->getContainer()->getFileManager()->locateJSAsset( 'admin/mce' );
33 }
34
35 return $plugins;
36 }, 10, 2 );
37
38 add_filter( 'mce_buttons', function ( $buttons ) {
39
40 $buttons = array_merge( $buttons, array_keys( $this->getAvailableVariables() ) );
41
42 return $buttons;
43 } );
44
45 add_action( 'woocommerce_admin_settings_sanitize_option', function ( $value, $option, $rawValue ) {
46
47 if ( self::FIELD_TYPE === $option['type'] ) {
48 return wp_kses_post( $rawValue );
49 }
50
51 return $value;
52 }, 10, 3 );
53
54 }
55
56 public function render( $value ) {
57 // the description is plugin-defined text (settings arrays), read once for output
58 $description = (string) ( $value['desc'] ?? '' );
59 if ( ! isset( $value['id'] ) ) {
60 $value['id'] = '';
61 }
62 if ( ! isset( $value['title'] ) ) {
63 $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
64 }
65 if ( ! isset( $value['default'] ) ) {
66 $value['default'] = '';
67 }
68 if ( ! isset( $value['desc'] ) ) {
69 $value['desc'] = '';
70 }
71 if ( ! isset( $value['desc_tip'] ) ) {
72 $value['desc_tip'] = false;
73 }
74 if ( ! isset( $value['placeholder'] ) ) {
75 $value['placeholder'] = '';
76 }
77 if ( ! isset( $value['value'] ) ) {
78 $value['value'] = \WC_Admin_Settings::get_option( $value['id'], $value['default'] );
79 }
80
81 $option_value = $value['value'];
82
83 $this->editorId = $value['id'];
84
85 $value['placeholders'] = is_array( $value['placeholders'] ) ? $value['placeholders'] : array();
86 ?>
87 <style>
88 #wp-<?php echo esc_html($value['id']); ?>-wrap .wp-editor-tools {
89 display: none;
90 }
91
92 #wp-<?php echo esc_html($value['id']); ?>-wrap {
93 max-width: 500px;
94 }
95 </style>
96 <tr valign="top">
97 <th scope="row" class="titledesc">
98 <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
99 </th>
100 <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
101 <?php
102 wp_editor( $option_value, $value['id'], array(
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,
129 ) );
130 ?>
131
132 <?php if ( $value['desc'] ) : ?>
133 <p class="description"><?php echo wp_kses_post( $description ); // nosemgrep ?></p>
134 <?php endif; ?>
135 </td>
136 </tr>
137 <?php
138 }
139
140 protected function getAvailableVariables() {
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 ) );
219 }
220 }
221