PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.25
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.25
6.35 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 All 141 releases
formidable / classes / models / FrmTableHTMLGenerator.php

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

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