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

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