PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.1
Tiered Pricing Table for WooCommerce v7.1.1
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 / Addons / RequestAQuote / Frontend / QuoteFormDisplay.php

QuoteFormDisplay.php in Tiered Pricing Table for WooCommerce 7.1.1, at src/Addons/RequestAQuote/Frontend/QuoteFormDisplay.php

259 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\RequestAQuote\Frontend;
2
3 use TierPricingTable\Addons\RequestAQuote\Models\RequestQuoteForm;
4 use TierPricingTable\Core\ServiceContainer;
5 use TierPricingTable\PriceManager;
6 use TierPricingTable\TierPricingTablePlugin;
7
8 class QuoteFormDisplay {
9
10 public static array $renderedModals = array();
11
12 public static function addModalToRender( $form, $productId ) {
13 self::$renderedModals[ $productId ] = $form;
14 }
15
16 public function __construct() {
17 // Initialize and register hooks for Layout Adapters
18 ( new Adapters\TableAdapter() )->registerHooks();
19 ( new Adapters\HorizontalTableAdapter() )->registerHooks();
20 ( new Adapters\BlocksAdapter() )->registerHooks();
21 ( new Adapters\OptionsAdapter() )->registerHooks();
22 ( new Adapters\DropdownAdapter() )->registerHooks();
23 ( new Adapters\PlainTextAdapter() )->registerHooks();
24 ( new Adapters\AddToCartAdapter() )->registerHooks();
25
26 add_action( 'wp_footer', array( $this, 'renderFormModal' ) );
27 add_action( 'wp_enqueue_scripts', array( $this, 'enqueueAssets' ) );
28 add_action( 'template_redirect', array( $this, 'handleSuccessNotice' ) );
29 }
30
31 public function handleSuccessNotice() {
32 if ( isset( $_GET['tier_pricing_table_quote_success'] ) && ! empty( $_GET['form_id'] ) ) {
33 $formId = sanitize_text_field( $_GET['form_id'] );
34 $form = RequestQuoteForm::get( $formId );
35 $message = __( 'Your quote request has been submitted successfully!', 'tier-pricing-table' );
36
37 if ( $form ) {
38 $message = $form->getSuccessMessage();
39 }
40
41 if ( function_exists( 'wc_add_notice' ) ) {
42 if ( function_exists( 'WC' ) && isset( WC()->session ) && ! WC()->session->has_session() ) {
43 WC()->session->set_customer_session_cookie( true );
44 }
45 wc_add_notice( $message, 'success' );
46 wp_safe_redirect( remove_query_arg( array( 'tier_pricing_table_quote_success', 'form_id' ) ) );
47 exit;
48 }
49 }
50 }
51
52 public function renderFormModal() {
53 if ( empty( self::$renderedModals ) ) {
54 return;
55 }
56
57 foreach ( self::$renderedModals as $productId => $form ) :
58 $product = wc_get_product( $productId );
59 if ( ! $product ) {
60 continue;
61 }
62 ServiceContainer::getInstance()->getFileManager()->includeTemplate( 'frontend/quote-modal.php', array(
63 'form' => $form,
64 'productId' => $productId,
65 'product' => $product,
66 'formDisplay' => $this,
67 ), plugin_dir_path( dirname( __FILE__ ) ) . 'views/' );
68 endforeach;
69 }
70
71 /**
72 * Render the form fields natively in PHP.
73 *
74 * @param RequestQuoteForm $form
75 * @param int $productId
76 */
77 public function renderFormFields( $form, $productId = 0 ) {
78 $fields = $form->getFields();
79 if ( empty( $fields ) ) {
80 return;
81 }
82
83 $currentUser = wp_get_current_user();
84 $defaultName = $currentUser->exists() ? $currentUser->display_name : '';
85 $defaultEmail = $currentUser->exists() ? $currentUser->user_email : '';
86
87 foreach ( $fields as $field ) {
88 $type = $field['type'] ?? 'text';
89 $name = $field['name'] ?? '';
90 $label = $field['label'] ?? '';
91 $placeholder = $field['placeholder'] ?? '';
92 $description = $field['description'] ?? '';
93 $required = ! empty( $field['required'] ) ? 'required' : '';
94 $optionsText = $field['options'] ?? '';
95
96 $options = array();
97 if ( $optionsText ) {
98 $options = array_filter( array_map( 'trim', explode( "\n", $optionsText ) ) );
99 }
100
101 $value = '';
102 if ( $name === 'name' ) {
103 $value = $defaultName;
104 } elseif ( $name === 'email' ) {
105 $value = $defaultEmail;
106 }
107
108 ?>
109 <div class="tpt-quote-field-wrapper">
110 <?php if ( $type === 'heading' ) : ?>
111 <h3 style="margin: 0 0 10px 0; color: inherit;"><?php echo esc_html( $label ); ?></h3>
112 <?php elseif ( $type === 'paragraph' ) : ?>
113 <p style="margin: 0 0 10px 0; color: inherit;"><?php echo wp_kses_post( $label ); ?></p>
114 <?php else : ?>
115 <?php
116 $titleFormat = $field['titleFormat'] ?? '';
117 if ( $titleFormat !== 'hidden' ) :
118 ?>
119 <label for="tpt_field_<?php echo esc_attr( $name ); ?>">
120 <?php echo esc_html( $label ); ?>
121 <?php if ( $required ) : ?>
122 <span class="required" style="color: #d63638; font-weight: normal; margin-left: 4px;"
123 title="required">*</span>
124 <?php endif; ?>
125 </label>
126 <?php endif; ?>
127
128 <?php if ( $type === 'select' ) : ?>
129 <select id="tpt_field_<?php echo esc_attr( $name ); ?>"
130 name="<?php echo esc_attr( $name ); ?>" <?php echo $required ? 'required' : ''; ?>>
131 <?php if ( ! empty( $field['hasPlaceholder'] ) && $placeholder ) : ?>
132 <option value="" disabled selected
133 hidden><?php echo esc_html( $placeholder ); ?></option>
134 <?php endif; ?>
135 <?php foreach ( $options as $opt ) : ?>
136 <option value="<?php echo esc_attr( $opt ); ?>"><?php echo esc_html( $opt ); ?></option>
137 <?php endforeach; ?>
138 </select>
139
140 <?php elseif ( $type === 'radio' ) : ?>
141 <div class="tpt-radio-group">
142 <?php foreach ( $options as $opt ) : ?>
143 <label>
144 <input type="radio" name="<?php echo esc_attr( $name ); ?>"
145 value="<?php echo esc_attr( $opt ); ?>" <?php echo $required ? 'required' : ''; ?>>
146 <?php echo esc_html( $opt ); ?>
147 </label>
148 <?php endforeach; ?>
149 </div>
150
151 <?php elseif ( $type === 'checkbox' ) : ?>
152 <div class="tpt-radio-group">
153 <?php
154 $isMultiple = count( $options ) > 1;
155 $chkName = $isMultiple ? $name . '[]' : $name;
156 $chkReq = $isMultiple ? '' : ( $required ? 'required' : '' );
157 foreach ( $options as $opt ) :
158 ?>
159 <label>
160 <input type="checkbox" name="<?php echo esc_attr( $chkName ); ?>"
161 value="<?php echo esc_attr( $opt ); ?>" <?php echo esc_attr( $chkReq ); ?>>
162 <?php echo esc_html( $opt ); ?>
163 </label>
164 <?php endforeach; ?>
165 </div>
166
167 <?php elseif ( $type === 'textarea' ) : ?>
168 <textarea id="tpt_field_<?php echo esc_attr( $name ); ?>"
169 name="<?php echo esc_attr( $name ); ?>"
170 maxlength="1500" <?php echo $required ? 'required' : ''; ?> <?php echo ( ! empty( $field['hasPlaceholder'] ) && $placeholder ) ? 'placeholder="' . esc_attr( $placeholder ) . '"' : ''; ?>><?php echo esc_textarea( $value ); ?></textarea>
171
172 <?php elseif ( $type === 'file' ) : ?>
173 <?php
174 $allowedTypes = ! empty( $field['allowedTypes'] ) ? esc_attr( '.' . str_replace( ',', ',.',
175 str_replace( ' ', '', $field['allowedTypes'] ) ) ) : '';
176 ?>
177 <input type="file" id="tpt_field_<?php echo esc_attr( $name ); ?>"
178 name="<?php echo esc_attr( $name ); ?>"
179 <?php echo $allowedTypes ? 'accept="' . $allowedTypes . '"' : ''; ?>
180 <?php echo $required ? 'required' : ''; ?>>
181
182 <?php else : ?>
183 <?php
184 $syncClass = ( $type === 'number' && ! empty( $field['syncWithQuantity'] ) ) ? 'tpt-quote-sync-quantity' : '';
185 $maxLengthAttr = in_array( $type,
186 array( 'text', 'email', 'tel', 'url' ) ) ? 'maxlength="255"' : '';
187
188 $min = 1;
189 if ( $type === 'number' && ! empty( $field['syncWithQuantity'] ) && $productId ) {
190 $pricingRule = PriceManager::getPricingRule( $productId );
191 $ruleMin = $pricingRule->getMinimum();
192 if ( $ruleMin > 0 ) {
193 $min = $ruleMin;
194 }
195 }
196
197 $minAttr = ( $type === 'number' ) ? 'min="' . esc_attr( $min ) . '"' : '';
198
199 if ( $type === 'date' && ! empty( $field['disablePastDates'] ) ) {
200 $minAttr = 'min="' . esc_attr( wp_date( 'Y-m-d' ) ) . '"';
201 }
202 ?>
203 <input type="<?php echo esc_attr( $type ); ?>" id="tpt_field_<?php echo esc_attr( $name ); ?>"
204 name="<?php echo esc_attr( $name ); ?>"
205 class="<?php echo esc_attr( $syncClass ); ?>"
206 value="<?php echo esc_attr( $value ); ?>" <?php echo $maxLengthAttr; ?> <?php echo $minAttr; ?> <?php echo $required ? 'required' : ''; ?> <?php echo ( ! empty( $field['hasPlaceholder'] ) && $placeholder ) ? 'placeholder="' . esc_attr( $placeholder ) . '"' : ''; ?>>
207 <?php endif; ?>
208
209 <?php if ( ! empty( $field['hasDescription'] ) && $description ) : ?>
210 <p class="tpt-field-description"
211 style="font-size: 12px; opacity: 0.8; margin: 4px 0 0 0;"><?php echo esc_html( $description ); ?></p>
212 <?php endif; ?>
213
214 <?php endif; ?>
215 </div>
216 <?php
217 }
218 }
219
220 function enqueueAssets() {
221 // Get forms
222 $formObjects = RequestQuoteForm::getAll();
223 $forms = array_map( function ( $form ) {
224 $data = $form->toArray();
225 unset( $data['fields'] );
226
227 return $data;
228 }, $formObjects );
229
230 // Check for reCAPTCHA
231 $globalSettings = get_option( 'tier_pricing_table_quote_global_settings', array() );
232 $siteKey = $globalSettings['recaptcha_site_key'] ?? null;
233
234 if ( ! empty( $siteKey ) && ! empty( $globalSettings['recaptcha_secret_key'] ) ) {
235 wp_enqueue_script( 'google-recaptcha-v3',
236 'https://www.google.com/recaptcha/api.js?render=' . esc_attr( $siteKey ), array(), null, true );
237 }
238
239 // Enqueue CSS
240 wp_enqueue_style( 'tpt-quote-form', plugins_url( '../assets/css/quote-form.css', __FILE__ ), array(),
241 TierPricingTablePlugin::VERSION );
242
243 // Enqueue JS
244 wp_enqueue_script( 'tpt-quote-form', plugins_url( '../assets/js/quote-form.js', __FILE__ ), array( 'jquery' ),
245 TierPricingTablePlugin::VERSION, true );
246
247 wp_localize_script( 'tpt-quote-form', 'tptQuoteFormConfig', array(
248 'forms' => $forms,
249 'restUrl' => esc_url_raw( rest_url( 'tier-pricing-table/v1/quote-request' ) ),
250 'nonce' => wp_create_nonce( 'wp_rest' ),
251 'i18n' => array(
252 'defaultModalTitle' => __( 'Request a Quote', 'tier-pricing-table' ),
253 'defaultSubmitText' => __( 'Submit Request', 'tier-pricing-table' ),
254 'defaultSubmittingText' => __( 'Submitting...', 'tier-pricing-table' ),
255 ),
256 ) );
257 }
258 }
259