| 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 |
if ( ! isset( $value['id'] ) ) { |
| 58 |
$value['id'] = ''; |
| 59 |
} |
| 60 |
if ( ! isset( $value['title'] ) ) { |
| 61 |
$value['title'] = isset( $value['name'] ) ? $value['name'] : ''; |
| 62 |
} |
| 63 |
if ( ! isset( $value['default'] ) ) { |
| 64 |
$value['default'] = ''; |
| 65 |
} |
| 66 |
if ( ! isset( $value['desc'] ) ) { |
| 67 |
$value['desc'] = ''; |
| 68 |
} |
| 69 |
if ( ! isset( $value['desc_tip'] ) ) { |
| 70 |
$value['desc_tip'] = false; |
| 71 |
} |
| 72 |
if ( ! isset( $value['placeholder'] ) ) { |
| 73 |
$value['placeholder'] = ''; |
| 74 |
} |
| 75 |
if ( ! isset( $value['value'] ) ) { |
| 76 |
$value['value'] = \WC_Admin_Settings::get_option( $value['id'], $value['default'] ); |
| 77 |
} |
| 78 |
|
| 79 |
$option_value = $value['value']; |
| 80 |
|
| 81 |
$this->editorId = $value['id']; |
| 82 |
|
| 83 |
$value['placeholders'] = is_array( $value['placeholders'] ) ? $value['placeholders'] : array(); |
| 84 |
?> |
| 85 |
<style> |
| 86 |
#wp-<?php echo esc_html($value['id']); ?>-wrap .wp-editor-tools { |
| 87 |
display: none; |
| 88 |
} |
| 89 |
|
| 90 |
#wp-<?php echo esc_html($value['id']); ?>-wrap { |
| 91 |
max-width: 500px; |
| 92 |
} |
| 93 |
</style> |
| 94 |
<tr valign="top"> |
| 95 |
<th scope="row" class="titledesc"> |
| 96 |
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label> |
| 97 |
</th> |
| 98 |
<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 99 |
<?php |
| 100 |
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, |
| 127 |
) ); |
| 128 |
?> |
| 129 |
|
| 130 |
<?php if ( $value['desc'] ) : ?> |
| 131 |
<p class="description"><?php echo wp_kses_post( $value['desc'] ); ?></p> |
| 132 |
<?php endif; ?> |
| 133 |
</td> |
| 134 |
</tr> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
protected function getAvailableVariables() { |
| 139 |
return apply_filters( 'tiered_pricing_table/text_template_variables', 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.', |
| 150 |
'tier-pricing-table' ), |
| 151 |
'variableKey' => '{tp_discount}', |
| 152 |
), |
| 153 |
|
| 154 |
'tp_rounded_discount' => array( |
| 155 |
'name' => __( 'Rounded percentage discount', 'tier-pricing-table' ), |
| 156 |
'description' => __( '{tp_rounded_discount} - current tier`s rounded percentage discount.', |
| 157 |
'tier-pricing-table' ), |
| 158 |
'variableKey' => '{tp_rounded_discount}', |
| 159 |
), |
| 160 |
|
| 161 |
'tp_required_quantity' => array( |
| 162 |
'name' => __( 'Required quantity', 'tier-pricing-table' ), |
| 163 |
'description' => __( '{tp_required_quantity} - required quantity to add a next tiered pricing.', |
| 164 |
'tier-pricing-table' ), |
| 165 |
'variableKey' => '{tp_required_quantity}', |
| 166 |
), |
| 167 |
|
| 168 |
'tp_next_price' => array( |
| 169 |
'name' => __( 'Next price', 'tier-pricing-table' ), |
| 170 |
'description' => __( '{tp_next_price} - next tiered pricing price user can get if they add required quantity.', |
| 171 |
'tier-pricing-table' ), |
| 172 |
'variableKey' => '{tp_next_price}', |
| 173 |
), |
| 174 |
|
| 175 |
'tp_next_discount' => array( |
| 176 |
'name' => __( 'Next discount', 'tier-pricing-table' ), |
| 177 |
'description' => __( '{tp_next_discount} - next discount if they add required quantity.', |
| 178 |
'tier-pricing-table' ), |
| 179 |
'variableKey' => '{tp_next_discount}', |
| 180 |
), |
| 181 |
|
| 182 |
'tp_actual_discount' => array( |
| 183 |
'name' => __( 'Next actual discount', 'tier-pricing-table' ), |
| 184 |
'description' => __( '{tp_actual_discount} - discount based on the original product price.', |
| 185 |
'tier-pricing-table' ), |
| 186 |
'variableKey' => '{tp_actual_discount}', |
| 187 |
), |
| 188 |
'tp_ys_price' => array( |
| 189 |
'name' => __( 'Save price', 'tier-pricing-table' ), |
| 190 |
'description' => __( '{tp_ys_price} - difference between regular and current price.', |
| 191 |
'tier-pricing-table' ), |
| 192 |
'variableKey' => '{tp_ys_price}', |
| 193 |
), |
| 194 |
'tp_ys_total_price' => array( |
| 195 |
'name' => __( 'Total save price', 'tier-pricing-table' ), |
| 196 |
'description' => __( '{tp_ys_total_price} - difference between regular and current price multiplied by quantity.', |
| 197 |
'tier-pricing-table' ), |
| 198 |
'variableKey' => '{tp_ys_total_price}', |
| 199 |
), |
| 200 |
'tp_ys_percentage_discount' => array( |
| 201 |
'name' => __( 'Percentage discount', 'tier-pricing-table' ), |
| 202 |
'variableKey' => '{tp_ys_percentage_discount}', |
| 203 |
), |
| 204 |
'tp_price' => array( |
| 205 |
'name' => __( 'Price', 'tier-pricing-table' ), |
| 206 |
'description' => __( '{tp_price} - Tier price for one piece', 'tier-pricing-table' ), |
| 207 |
'variableKey' => '{tp_price}', |
| 208 |
), |
| 209 |
'tp_base_unit_name' => array( |
| 210 |
'name' => __( 'Unit label', 'tier-pricing-table' ), |
| 211 |
'description' => __( 'It will use Unit label from products if you set it.', |
| 212 |
'tier-pricing-table' ), |
| 213 |
'variableKey' => '{tp_base_unit_name}', |
| 214 |
), |
| 215 |
|
| 216 |
) ); |
| 217 |
} |
| 218 |
} |
| 219 |
|