PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16.2
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.16.2, at classes/models/FrmTableHTMLGenerator.php

353 lines 7.4 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 * Used to add a class in tables. Set in Pro.
58 *
59 * @var bool
60 * @since 5.4.2
61 */
62 public $is_child = false;
63
64 /**
65 * FrmTableHTMLGenerator constructor.
66 *
67 * @param string $type
68 * @param array $atts
69 */
70 public function __construct( $type, $atts = array() ) {
71
72 $this->type = (string) $type;
73 $this->init_style_settings( $atts );
74 $this->init_use_inline_style( $atts );
75 $this->init_direction( $atts );
76 $this->init_table_style();
77 $this->init_td_style();
78 }
79
80 /**
81 * Set the style_settings property
82 *
83 * @since 2.04
84 *
85 * @param array $atts
86 * @return void
87 */
88 private function init_style_settings( $atts ) {
89 $style_settings = array(
90 'border_color' => 'dddddd',
91 'bg_color' => 'f7f7f7',
92 'text_color' => '444444',
93 'font_size' => '12px',
94 'border_width' => '1px',
95 'alt_bg_color' => 'ffffff',
96 );
97 $this->style_settings = apply_filters( 'frm_show_entry_styles', $style_settings );
98
99 foreach ( $this->style_settings as $key => $setting ) {
100 if ( isset( $atts[ $key ] ) && $atts[ $key ] !== '' ) {
101 $this->style_settings[ $key ] = $atts[ $key ];
102 }
103
104 if ( $this->is_color_setting( $key ) ) {
105 $this->style_settings[ $key ] = $this->get_color_markup( $this->style_settings[ $key ] );
106 }
107 }
108
109 $this->style_settings['class'] = isset( $atts['class'] ) ? $atts['class'] : '';
110 }
111
112 /**
113 * Set the use_inline_style property
114 *
115 * @since 2.04
116 *
117 * @param array $atts
118 * @return void
119 */
120 private function init_use_inline_style( $atts ) {
121 if ( isset( $atts['inline_style'] ) && ! $atts['inline_style'] ) {
122 $this->use_inline_style = false;
123 }
124 }
125
126 /**
127 * Set the direction property
128 *
129 * @since 2.04
130 *
131 * @param array $atts
132 * @return void
133 */
134 private function init_direction( $atts ) {
135 if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
136 $this->direction = 'rtl';
137 }
138 }
139
140 /**
141 * Set the table_style property
142 *
143 * @since 2.04
144 * @return void
145 */
146 private function init_table_style() {
147 if ( $this->use_inline_style === true ) {
148
149 $this->table_style = ' style="' . esc_attr( 'font-size:' . $this->style_settings['font_size'] . ';line-height:135%;' );
150 $this->table_style .= esc_attr( 'border-bottom:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';' ) . '"';
151
152 }
153
154 if ( ! empty( $this->style_settings['class'] ) ) {
155 $this->table_style .= ' class="' . esc_attr( $this->style_settings['class'] ) . '"';
156 }
157 }
158
159 /**
160 * Set the td_style property
161 *
162 * @since 2.04
163 * @return void
164 */
165 private function init_td_style() {
166 if ( $this->use_inline_style === true ) {
167
168 $td_style_attributes = 'text-align:' . ( $this->direction === 'rtl' ? 'right' : 'left' ) . ';';
169 $td_style_attributes .= 'color:' . $this->style_settings['text_color'] . ';padding:7px 9px;vertical-align:top;';
170 $td_style_attributes .= 'border-top:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';';
171
172 $this->td_style = ' style="' . $td_style_attributes . '"';
173 }
174 }
175
176 /**
177 * Determine if setting is for a color, e.g. text color, background color, or border color
178 *
179 * @since 2.05
180 * @param string $setting_key Name of setting.
181 *
182 * @return bool
183 */
184 private function is_color_setting( $setting_key ) {
185 return strpos( $setting_key, 'color' ) !== false;
186 }
187
188 /**
189 * Get color markup from color setting value
190 *
191 * @since 2.05
192 * @param string $color_markup value of a color setting, with format #FFFFF, FFFFFF, or white.
193 *
194 * @return string
195 */
196 private function get_color_markup( $color_markup ) {
197 $color_markup = trim( $color_markup );
198
199 // Check if each character in string is valid hex digit
200 if ( FrmAppHelper::ctype_xdigit( $color_markup ) ) {
201 $color_markup = '#' . $color_markup;
202 }
203
204 return $color_markup;
205 }
206
207 /**
208 * Get the table row background color
209 *
210 * @since 2.04
211 *
212 * @return string
213 */
214 private function table_row_background_color() {
215 return ( $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color'] );
216 }
217
218 /**
219 * Get the table row style
220 *
221 * @since 2.04
222 * @since 5.0.16 Changed scope from `private` to `protected`.
223 *
224 * @return string
225 */
226 protected function tr_style() {
227
228 if ( $this->type === 'shortcode' ) {
229 $tr_style = ' style="[frm-alt-color]"';
230 } elseif ( $this->use_inline_style ) {
231 $tr_style = ' style="background-color:' . $this->table_row_background_color() . ';"';
232 } else {
233 $tr_style = '';
234 }
235
236 return $tr_style;
237 }
238
239 /**
240 * Switch the odd property from true to false or false to true
241 *
242 * @since 2.04
243 * @since 5.0.16 Changed scope from `private` to `protected`.
244 *
245 * @return void
246 */
247 protected function switch_odd() {
248 if ( $this->type !== 'shortcode' ) {
249 $this->odd = ! $this->odd;
250 }
251 }
252
253 /**
254 * Generate a table header
255 *
256 * @since 2.04
257 *
258 * @return string
259 */
260 public function generate_table_header() {
261 return '<table cellspacing="0" ' . $this->table_style . '><tbody>' . "\r\n";
262 }
263
264 /**
265 * Generate a table footer
266 *
267 * @since 2.04
268 *
269 * @return string
270 */
271 public function generate_table_footer() {
272 return '</tbody></table>';
273 }
274
275 /**
276 * Generate a two cell row for an HTML table
277 *
278 * @since 2.04
279 *
280 * @param string $label The label.
281 * @param string $value The value.
282 *
283 * @return string
284 */
285 public function generate_two_cell_table_row( $label, $value ) {
286 $row = '<tr' . $this->tr_style();
287 $row .= $this->add_row_class( $value === '' );
288 $row .= '>';
289
290 $label = '<th' . $this->td_style . '>' . wp_kses_post( $label ) . '</th>';
291 $value = '<td' . $this->td_style . '>' . wp_kses_post( $value ) . '</td>';
292
293 if ( 'rtl' == $this->direction ) {
294 $row .= $value;
295 $row .= $label;
296 } else {
297 $row .= $label;
298 $row .= $value;
299 }
300
301 $row .= '</tr>' . "\r\n";
302
303 $this->switch_odd();
304
305 return $row;
306 }
307
308 /**
309 * Generate a single cell row for an HTML table
310 *
311 * @since 2.04
312 *
313 * @param string $value
314 *
315 * @return string
316 */
317 public function generate_single_cell_table_row( $value ) {
318 $row = '<tr' . $this->tr_style();
319 $row .= $this->add_row_class();
320 $row .= '>';
321 $row .= '<td colspan="2"' . $this->td_style . '>' . $value . '</td>';
322 $row .= '</tr>' . "\r\n";
323
324 $this->switch_odd();
325
326 return $row;
327 }
328
329 /**
330 * Add classes to the tr.
331 *
332 * @since 5.4.2
333 *
334 * @param bool $empty If the value in the row is blank.
335 *
336 * @return string
337 */
338 protected function add_row_class( $empty = false ) {
339 $class = '';
340 if ( $empty ) {
341 // Only add this class on two cell rows.
342 $class .= ' frm-empty-row';
343 }
344 if ( $this->is_child ) {
345 $class .= ' frm-child-row';
346 }
347 if ( $class ) {
348 $class = ' class="' . trim( $class ) . '"';
349 }
350 return $class;
351 }
352 }
353