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

355 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 * @param string $setting_key name of setting
180 *
181 * @since 2.05
182 *
183 * @return boolean
184 */
185 private function is_color_setting( $setting_key ) {
186 return strpos( $setting_key, 'color' ) !== false;
187 }
188
189 /**
190 * Get color markup from color setting value
191 *
192 * @param string $color_markup value of a color setting, with format #FFFFF, FFFFFF, or white.
193 *
194 * @since 2.05
195 *
196 * @return string
197 */
198 private function get_color_markup( $color_markup ) {
199 $color_markup = trim( $color_markup );
200
201 // Check if each character in string is valid hex digit
202 if ( FrmAppHelper::ctype_xdigit( $color_markup ) ) {
203 $color_markup = '#' . $color_markup;
204 }
205
206 return $color_markup;
207 }
208
209 /**
210 * Get the table row background color
211 *
212 * @since 2.04
213 *
214 * @return string
215 */
216 private function table_row_background_color() {
217 return ( $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color'] );
218 }
219
220 /**
221 * Get the table row style
222 *
223 * @since 2.04
224 * @since 5.0.16 Changed scope from `private` to `protected`.
225 *
226 * @return string
227 */
228 protected function tr_style() {
229
230 if ( $this->type === 'shortcode' ) {
231 $tr_style = ' style="[frm-alt-color]"';
232 } elseif ( $this->use_inline_style ) {
233 $tr_style = ' style="background-color:' . $this->table_row_background_color() . ';"';
234 } else {
235 $tr_style = '';
236 }
237
238 return $tr_style;
239 }
240
241 /**
242 * Switch the odd property from true to false or false to true
243 *
244 * @since 2.04
245 * @since 5.0.16 Changed scope from `private` to `protected`.
246 *
247 * @return void
248 */
249 protected function switch_odd() {
250 if ( $this->type !== 'shortcode' ) {
251 $this->odd = ! $this->odd;
252 }
253 }
254
255 /**
256 * Generate a table header
257 *
258 * @since 2.04
259 *
260 * @return string
261 */
262 public function generate_table_header() {
263 return '<table cellspacing="0" ' . $this->table_style . '><tbody>' . "\r\n";
264 }
265
266 /**
267 * Generate a table footer
268 *
269 * @since 2.04
270 *
271 * @return string
272 */
273 public function generate_table_footer() {
274 return '</tbody></table>';
275 }
276
277 /**
278 * Generate a two cell row for an HTML table
279 *
280 * @since 2.04
281 *
282 * @param string $label The label.
283 * @param string $value The value.
284 *
285 * @return string
286 */
287 public function generate_two_cell_table_row( $label, $value ) {
288 $row = '<tr' . $this->tr_style();
289 $row .= $this->add_row_class( $value === '' );
290 $row .= '>';
291
292 $label = '<th' . $this->td_style . '>' . wp_kses_post( $label ) . '</th>';
293 $value = '<td' . $this->td_style . '>' . wp_kses_post( $value ) . '</td>';
294
295 if ( 'rtl' == $this->direction ) {
296 $row .= $value;
297 $row .= $label;
298 } else {
299 $row .= $label;
300 $row .= $value;
301 }
302
303 $row .= '</tr>' . "\r\n";
304
305 $this->switch_odd();
306
307 return $row;
308 }
309
310 /**
311 * Generate a single cell row for an HTML table
312 *
313 * @since 2.04
314 *
315 * @param string $value
316 *
317 * @return string
318 */
319 public function generate_single_cell_table_row( $value ) {
320 $row = '<tr' . $this->tr_style();
321 $row .= $this->add_row_class();
322 $row .= '>';
323 $row .= '<td colspan="2"' . $this->td_style . '>' . $value . '</td>';
324 $row .= '</tr>' . "\r\n";
325
326 $this->switch_odd();
327
328 return $row;
329 }
330
331 /**
332 * Add classes to the tr.
333 *
334 * @since 5.4.2
335 *
336 * @param bool $empty If the value in the row is blank.
337 *
338 * @return string
339 */
340 protected function add_row_class( $empty = false ) {
341 $class = '';
342 if ( $empty ) {
343 // Only add this class on two cell rows.
344 $class .= ' frm-empty-row';
345 }
346 if ( $this->is_child ) {
347 $class .= ' frm-child-row';
348 }
349 if ( $class ) {
350 $class = ' class="' . trim( $class ) . '"';
351 }
352 return $class;
353 }
354 }
355