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