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

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