PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.34
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.34
6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 6.5.4 All 140 releases
formidable / classes / models / FrmTableHTMLGenerator.php

FrmTableHTMLGenerator.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.34, at classes/models/FrmTableHTMLGenerator.php

455 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 2.04
8 */
9 class FrmTableHTMLGenerator {
10
11 /**
12 * @var string
13 *
14 * @since 2.04
15 */
16 private $type = '';
17
18 /**
19 * @var array
20 *
21 * @since 2.04
22 */
23 private $style_settings = array();
24
25 /**
26 * @var bool
27 *
28 * @since 2.04
29 */
30 private $use_inline_style = true;
31
32 /**
33 * @var string
34 *
35 * @since 2.04
36 * @since 5.0.16 Changed scope from `private` to `protected`.
37 */
38 protected $direction = 'ltr';
39
40 /**
41 * @var bool
42 *
43 * @since 2.04
44 */
45 private $odd = true;
46
47 /**
48 * @var string
49 *
50 * @since 2.04
51 * @since 5.0.16 Changed scope from `private` to `protected`.
52 */
53 protected $table_style = '';
54
55 /**
56 * @var string
57 *
58 * @since 2.04
59 * @since 5.0.16 Changed scope from `private` to `protected`.
60 */
61 protected $td_style = '';
62
63 /**
64 * Cell padding.
65 *
66 * @since 6.25
67 *
68 * @var string
69 */
70 protected $cell_padding = '7px 9px';
71
72 /**
73 * Table width.
74 *
75 * @since 6.25
76 *
77 * @var string
78 */
79 protected $width = '';
80
81 /**
82 * Used to add a class in tables. Set in Pro.
83 *
84 * @var bool
85 *
86 * @since 5.4.2
87 */
88 public $is_child = false;
89
90 /**
91 * FrmTableHTMLGenerator constructor.
92 *
93 * @param string $type
94 * @param array $atts
95 */
96 public function __construct( $type, $atts = array() ) {
97 $this->type = $type;
98
99 if ( isset( $atts['cell_padding'] ) ) {
100 $this->cell_padding = $atts['cell_padding'];
101 }
102
103 if ( isset( $atts['width'] ) ) {
104 $this->width = $atts['width'];
105 }
106
107 $this->init_style_settings( $atts );
108 $this->init_use_inline_style( $atts );
109 $this->init_direction( $atts );
110 $this->init_table_style();
111 $this->init_td_style();
112 }
113
114 /**
115 * Set the style_settings property
116 *
117 * @since 2.04
118 *
119 * @param array $atts
120 *
121 * @return void
122 */
123 private function init_style_settings( $atts ) {
124 $style_settings = array(
125 'border_color' => 'dddddd',
126 'bg_color' => 'f7f7f7',
127 'text_color' => '444444',
128 'font_size' => '12px',
129 'border_width' => '1px',
130 'alt_bg_color' => 'ffffff',
131 );
132 $this->style_settings = apply_filters( 'frm_show_entry_styles', $style_settings );
133
134 foreach ( $this->style_settings as $key => $setting ) {
135 if ( isset( $atts[ $key ] ) && $atts[ $key ] !== '' ) {
136 $this->style_settings[ $key ] = $atts[ $key ];
137 }
138
139 if ( $this->is_color_setting( $key ) ) {
140 $this->style_settings[ $key ] = $this->get_color_markup( $this->style_settings[ $key ] );
141 }
142 }
143
144 $this->style_settings['class'] = $atts['class'] ?? '';
145 }
146
147 /**
148 * Set the use_inline_style property
149 *
150 * @since 2.04
151 *
152 * @param array $atts
153 *
154 * @return void
155 */
156 private function init_use_inline_style( $atts ) {
157 if ( isset( $atts['inline_style'] ) && ! $atts['inline_style'] ) {
158 $this->use_inline_style = false;
159 }
160 }
161
162 /**
163 * Set the direction property
164 *
165 * @since 2.04
166 *
167 * @param array $atts
168 *
169 * @return void
170 */
171 private function init_direction( $atts ) {
172 if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
173 $this->direction = 'rtl';
174 }
175 }
176
177 /**
178 * Set the table_style property
179 *
180 * @since 2.04
181 *
182 * @return void
183 */
184 private function init_table_style() {
185 if ( $this->use_inline_style === true ) {
186 $this->table_style = ' style="' . esc_attr( 'border-spacing:0;font-size:' . $this->style_settings['font_size'] . ';line-height:135%;' );
187 $this->table_style .= esc_attr( 'border-bottom:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';' ) . '"';
188 }
189
190 if ( ! empty( $this->style_settings['class'] ) ) {
191 $this->table_style .= ' class="' . esc_attr( $this->style_settings['class'] ) . '"';
192 }
193
194 if ( $this->width ) {
195 $this->table_style .= ' width="' . esc_attr( $this->width ) . '"';
196 }
197 }
198
199 /**
200 * Set the td_style property
201 *
202 * @since 2.04
203 *
204 * @return void
205 */
206 private function init_td_style() {
207 if ( $this->use_inline_style !== true ) {
208 return;
209 }
210
211 $td_style_attributes = 'text-align:' . ( $this->direction === 'rtl' ? 'right' : 'left' ) . ';';
212 $td_style_attributes .= 'color:' . $this->style_settings['text_color'] . ';padding:' . $this->cell_padding . ';vertical-align:top;';
213 $td_style_attributes .= 'border-top:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';';
214
215 $this->td_style = ' style="' . esc_attr( $td_style_attributes ) . '"';
216 }
217
218 /**
219 * Removes border CSS from HTML.
220 *
221 * @since 6.25
222 *
223 * @param string $html The HTML.
224 * @param string $position The border position. Default is `top`.
225 *
226 * @return string
227 */
228 public function remove_border( $html, $position = 'top' ) {
229 $search = sprintf(
230 'border-%1$s:%2$s solid %3$s;',
231 $position,
232 $this->style_settings['border_width'],
233 $this->style_settings['border_color']
234 );
235
236 return str_replace( $search, '', $html );
237 }
238
239 /**
240 * Determine if setting is for a color, e.g. text color, background color, or border color
241 *
242 * @since 2.05
243 *
244 * @param string $setting_key Name of setting.
245 *
246 * @return bool
247 */
248 private function is_color_setting( $setting_key ) {
249 return str_contains( $setting_key, 'color' );
250 }
251
252 /**
253 * Get color markup from color setting value
254 *
255 * @since 2.05
256 *
257 * @param string $color_markup value of a color setting, with format #FFFFF, FFFFFF, or white.
258 *
259 * @return string
260 */
261 private function get_color_markup( $color_markup ) {
262 $color_markup = trim( $color_markup );
263
264 // Check if each character in string is valid hex digit
265 if ( FrmAppHelper::ctype_xdigit( $color_markup ) ) {
266 return '#' . $color_markup;
267 }
268
269 return $color_markup;
270 }
271
272 /**
273 * Get the table row background color
274 *
275 * @since 2.04
276 *
277 * @return string
278 */
279 private function table_row_background_color() {
280 return $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color'];
281 }
282
283 /**
284 * Get the table row style
285 *
286 * @since 2.04
287 * @since 5.0.16 Changed scope from `private` to `protected`.
288 * @since 6.25 Changed scope from `protected` to `public`.
289 *
290 * @return string
291 */
292 public function tr_style() {
293 if ( $this->type === 'shortcode' ) {
294 $tr_style = ' style="[frm-alt-color]"';
295 } elseif ( $this->use_inline_style ) {
296 $tr_style = ' style="background-color:' . $this->table_row_background_color() . ';"';
297 } else {
298 $tr_style = '';
299 }
300
301 return $tr_style;
302 }
303
304 /**
305 * Gets table style.
306 *
307 * @since 6.25
308 *
309 * @return string
310 */
311 public function get_table_style() {
312 return $this->table_style;
313 }
314
315 /**
316 * Gets td style.
317 *
318 * @since 6.25
319 *
320 * @return string
321 */
322 public function get_td_style() {
323 return $this->td_style;
324 }
325
326 /**
327 * Switch the odd property from true to false or false to true
328 *
329 * @since 2.04
330 * @since 5.0.16 Changed scope from `private` to `protected`.
331 *
332 * @return void
333 */
334 protected function switch_odd() {
335 if ( $this->type !== 'shortcode' ) {
336 $this->odd = ! $this->odd;
337 }
338 }
339
340 /**
341 * Generate a table header
342 *
343 * @since 2.04
344 *
345 * @return string
346 */
347 public function generate_table_header() {
348 return '<table cellspacing="0"' . $this->table_style . '><tbody>' . "\r\n";
349 }
350
351 /**
352 * Generate a table footer
353 *
354 * @since 2.04
355 *
356 * @return string
357 */
358 public function generate_table_footer() {
359 return '</tbody></table>';
360 }
361
362 /**
363 * Generate a two cell row for an HTML table
364 *
365 * @since 2.04
366 *
367 * @param string $label The label.
368 * @param string $value The value.
369 * @param array $args Additional arguments. May include "field_type" string.
370 *
371 * @return string
372 */
373 public function generate_two_cell_table_row( $label, $value, $args = array() ) {
374 $row = '<tr' . $this->tr_style();
375 $row .= $this->add_row_class( $value === '' );
376 $row .= '>';
377 $label = '<th scope="row"' . $this->td_style . '>' . wp_kses_post( $label ) . '</th>';
378 $value = '<td' . $this->td_style . '>' . $this->filter_value_for_display( $value, $args ) . '</td>';
379
380 if ( 'rtl' === $this->direction ) {
381 $row .= $value;
382 $row .= $label;
383 } else {
384 $row .= $label;
385 $row .= $value;
386 }
387
388 $row .= '</tr>' . "\r\n";
389
390 $this->switch_odd();
391
392 return $row;
393 }
394
395 /**
396 * @param string $value
397 * @param array $args
398 *
399 * @return string
400 */
401 private function filter_value_for_display( $value, $args ) {
402 if ( empty( $args['field_type'] ) ) {
403 return wp_kses_post( $value );
404 }
405
406 $field_object = FrmFieldFactory::get_field_type( $args['field_type'] );
407 return $field_object->filter_value_for_table_html( $value );
408 }
409
410 /**
411 * Generate a single cell row for an HTML table
412 *
413 * @since 2.04
414 *
415 * @param string $value
416 *
417 * @return string
418 */
419 public function generate_single_cell_table_row( $value ) {
420 $row = '<tr' . $this->tr_style();
421 $row .= $this->add_row_class();
422 $row .= '>';
423 $row .= '<td colspan="2"' . $this->td_style . '>' . $value . '</td>';
424 $row .= '</tr>' . "\r\n";
425
426 $this->switch_odd();
427
428 return $row;
429 }
430
431 /**
432 * Add classes to the tr.
433 *
434 * @since 5.4.2
435 *
436 * @param bool $empty If the value in the row is blank.
437 *
438 * @return string
439 */
440 protected function add_row_class( $empty = false ) {
441 $class = '';
442
443 if ( $empty ) {
444 // Only add this class on two cell rows.
445 $class .= ' frm-empty-row';
446 }
447
448 if ( $this->is_child ) {
449 $class .= ' frm-child-row';
450 }
451
452 return $class ? ' class="' . trim( $class ) . '"' : $class;
453 }
454 }
455