| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | class FrmTableHTMLGenerator { |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * @var string |
| 13 | + * | |
| 13 | 14 | * @since 2.04 |
| 14 | 15 | */ |
| 15 | 16 | private $type = ''; |
| 16 | 17 | |
| @@ -15,8 +16,9 @@ | ||
| 15 | 16 | private $type = ''; |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | 19 | * @var array |
| 20 | + * | |
| 19 | 21 | * @since 2.04 |
| 20 | 22 | */ |
| 21 | 23 | private $style_settings = array(); |
| 22 | 24 | |
| @@ -21,8 +23,9 @@ | ||
| 21 | 23 | private $style_settings = array(); |
| 22 | 24 | |
| 23 | 25 | /** |
| 24 | 26 | * @var bool |
| 27 | + * | |
| 25 | 28 | * @since 2.04 |
| 26 | 29 | */ |
| 27 | 30 | private $use_inline_style = true; |
| 28 | 31 | |
| @@ -27,8 +30,9 @@ | ||
| 27 | 30 | private $use_inline_style = true; |
| 28 | 31 | |
| 29 | 32 | /** |
| 30 | 33 | * @var string |
| 34 | + * | |
| 31 | 35 | * @since 2.04 |
| 32 | 36 | * @since 5.0.16 Changed scope from `private` to `protected`. |
| 33 | 37 | */ |
| 34 | 38 | protected $direction = 'ltr'; |
| @@ -34,8 +38,9 @@ | ||
| 34 | 38 | protected $direction = 'ltr'; |
| 35 | 39 | |
| 36 | 40 | /** |
| 37 | 41 | * @var bool |
| 42 | + * | |
| 38 | 43 | * @since 2.04 |
| 39 | 44 | */ |
| 40 | 45 | private $odd = true; |
| 41 | 46 | |
| @@ -40,8 +45,9 @@ | ||
| 40 | 45 | private $odd = true; |
| 41 | 46 | |
| 42 | 47 | /** |
| 43 | 48 | * @var string |
| 49 | + * | |
| 44 | 50 | * @since 2.04 |
| 45 | 51 | * @since 5.0.16 Changed scope from `private` to `protected`. |
| 46 | 52 | */ |
| 47 | 53 | protected $table_style = ''; |
| @@ -47,8 +53,9 @@ | ||
| 47 | 53 | protected $table_style = ''; |
| 48 | 54 | |
| 49 | 55 | /** |
| 50 | 56 | * @var string |
| 57 | + * | |
| 51 | 58 | * @since 2.04 |
| 52 | 59 | * @since 5.0.16 Changed scope from `private` to `protected`. |
| 53 | 60 | */ |
| 54 | 61 | protected $td_style = ''; |
| @@ -53,11 +60,30 @@ | ||
| 53 | 60 | */ |
| 54 | 61 | protected $td_style = ''; |
| 55 | 62 | |
| 56 | 63 | /** |
| 64 | + * Cell padding. | |
| 65 | + * | |
| 66 | + * @since 6.25 | |
| 67 | + * | |
| 68 | + * @var string | |
| 69 | + */ | |
| 70 | + protected $cell_padding = '7px 9px'; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Table width. | |
| 74 | + * | |
| 75 | + * @since 6.25 | |
| 76 | + * | |
| 77 | + * @var string | |
| 78 | + */ | |
| 79 | + protected $width = ''; | |
| 80 | + | |
| 81 | + /** | |
| 57 | 82 | * Used to add a class in tables. Set in Pro. |
| 58 | 83 | * |
| 59 | 84 | * @var bool |
| 85 | + * | |
| 60 | 86 | * @since 5.4.2 |
| 61 | 87 | */ |
| 62 | 88 | public $is_child = false; |
| 63 | 89 | |
| @@ -64,13 +90,21 @@ | ||
| 64 | 90 | /** |
| 65 | 91 | * FrmTableHTMLGenerator constructor. |
| 66 | 92 | * |
| 67 | 93 | * @param string $type |
| 68 | - * @param array $atts | |
| 94 | + * @param array $atts | |
| 69 | 95 | */ |
| 70 | 96 | public function __construct( $type, $atts = array() ) { |
| 97 | + $this->type = $type; | |
| 71 | 98 | |
| 72 | - $this->type = (string) $type; | |
| 99 | + if ( isset( $atts['cell_padding'] ) ) { | |
| 100 | + $this->cell_padding = $atts['cell_padding']; | |
| 101 | + } | |
| 102 | + | |
| 103 | + if ( isset( $atts['width'] ) ) { | |
| 104 | + $this->width = $atts['width']; | |
| 105 | + } | |
| 106 | + | |
| 73 | 107 | $this->init_style_settings( $atts ); |
| 74 | 108 | $this->init_use_inline_style( $atts ); |
| 75 | 109 | $this->init_direction( $atts ); |
| 76 | 110 | $this->init_table_style(); |
| @@ -82,8 +116,9 @@ | ||
| 82 | 116 | * |
| 83 | 117 | * @since 2.04 |
| 84 | 118 | * |
| 85 | 119 | * @param array $atts |
| 120 | + * | |
| 86 | 121 | * @return void |
| 87 | 122 | */ |
| 88 | 123 | private function init_style_settings( $atts ) { |
| 89 | 124 | $style_settings = array( |
| @@ -105,9 +140,9 @@ | ||
| 105 | 140 | $this->style_settings[ $key ] = $this->get_color_markup( $this->style_settings[ $key ] ); |
| 106 | 141 | } |
| 107 | 142 | } |
| 108 | 143 | |
| 109 | - $this->style_settings['class'] = isset( $atts['class'] ) ? $atts['class'] : ''; | |
| 144 | + $this->style_settings['class'] = $atts['class'] ?? ''; | |
| 110 | 145 | } |
| 111 | 146 | |
| 112 | 147 | /** |
| 113 | 148 | * Set the use_inline_style property |
| @@ -114,8 +149,9 @@ | ||
| 114 | 149 | * |
| 115 | 150 | * @since 2.04 |
| 116 | 151 | * |
| 117 | 152 | * @param array $atts |
| 153 | + * | |
| 118 | 154 | * @return void |
| 119 | 155 | */ |
| 120 | 156 | private function init_use_inline_style( $atts ) { |
| 121 | 157 | if ( isset( $atts['inline_style'] ) && ! $atts['inline_style'] ) { |
| @@ -128,8 +164,9 @@ | ||
| 128 | 164 | * |
| 129 | 165 | * @since 2.04 |
| 130 | 166 | * |
| 131 | 167 | * @param array $atts |
| 168 | + * | |
| 132 | 169 | * @return void |
| 133 | 170 | */ |
| 134 | 171 | private function init_direction( $atts ) { |
| 135 | 172 | if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) { |
| @@ -140,21 +177,24 @@ | ||
| 140 | 177 | /** |
| 141 | 178 | * Set the table_style property |
| 142 | 179 | * |
| 143 | 180 | * @since 2.04 |
| 181 | + * | |
| 144 | 182 | * @return void |
| 145 | 183 | */ |
| 146 | 184 | private function init_table_style() { |
| 147 | 185 | if ( $this->use_inline_style === true ) { |
| 148 | - | |
| 149 | - $this->table_style = ' style="' . esc_attr( 'font-size:' . $this->style_settings['font_size'] . ';line-height:135%;' ); | |
| 186 | + $this->table_style = ' style="' . esc_attr( 'border-spacing:0;font-size:' . $this->style_settings['font_size'] . ';line-height:135%;' ); | |
| 150 | 187 | $this->table_style .= esc_attr( 'border-bottom:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';' ) . '"'; |
| 151 | - | |
| 152 | 188 | } |
| 153 | 189 | |
| 154 | 190 | if ( ! empty( $this->style_settings['class'] ) ) { |
| 155 | 191 | $this->table_style .= ' class="' . esc_attr( $this->style_settings['class'] ) . '"'; |
| 156 | 192 | } |
| 193 | + | |
| 194 | + if ( $this->width ) { | |
| 195 | + $this->table_style .= ' width="' . esc_attr( $this->width ) . '"'; | |
| 196 | + } | |
| 157 | 197 | } |
| 158 | 198 | |
| 159 | 199 | /** |
| 160 | 200 | * Set the td_style property |
| @@ -159,41 +199,64 @@ | ||
| 159 | 199 | /** |
| 160 | 200 | * Set the td_style property |
| 161 | 201 | * |
| 162 | 202 | * @since 2.04 |
| 203 | + * | |
| 163 | 204 | * @return void |
| 164 | 205 | */ |
| 165 | 206 | private function init_td_style() { |
| 166 | - if ( $this->use_inline_style === true ) { | |
| 207 | + if ( $this->use_inline_style !== true ) { | |
| 208 | + return; | |
| 209 | + } | |
| 167 | 210 | |
| 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'] . ';'; | |
| 211 | + $td_style_attributes = 'text-align:' . ( $this->direction === 'rtl' ? 'right' : 'left' ) . ';'; | |
| 212 | + $td_style_attributes .= 'color:' . $this->style_settings['text_color'] . ';padding:' . $this->cell_padding . ';vertical-align:top;'; | |
| 213 | + $td_style_attributes .= 'border-top:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';'; | |
| 171 | 214 | |
| 172 | - $this->td_style = ' style="' . $td_style_attributes . '"'; | |
| 173 | - } | |
| 215 | + $this->td_style = ' style="' . esc_attr( $td_style_attributes ) . '"'; | |
| 174 | 216 | } |
| 175 | 217 | |
| 176 | 218 | /** |
| 219 | + * Removes border CSS from HTML. | |
| 220 | + * | |
| 221 | + * @since 6.25 | |
| 222 | + * | |
| 223 | + * @param string $html The HTML. | |
| 224 | + * @param string $position The border position. Default is `top`. | |
| 225 | + * | |
| 226 | + * @return string | |
| 227 | + */ | |
| 228 | + public function remove_border( $html, $position = 'top' ) { | |
| 229 | + $search = sprintf( | |
| 230 | + 'border-%1$s:%2$s solid %3$s;', | |
| 231 | + $position, | |
| 232 | + $this->style_settings['border_width'], | |
| 233 | + $this->style_settings['border_color'] | |
| 234 | + ); | |
| 235 | + | |
| 236 | + return str_replace( $search, '', $html ); | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 177 | 240 | * Determine if setting is for a color, e.g. text color, background color, or border color |
| 178 | 241 | * |
| 179 | - * @param string $setting_key name of setting | |
| 242 | + * @since 2.05 | |
| 180 | 243 | * |
| 181 | - * @since 2.05 | |
| 244 | + * @param string $setting_key Name of setting. | |
| 182 | 245 | * |
| 183 | - * @return boolean | |
| 246 | + * @return bool | |
| 184 | 247 | */ |
| 185 | 248 | private function is_color_setting( $setting_key ) { |
| 186 | - return strpos( $setting_key, 'color' ) !== false; | |
| 249 | + return str_contains( $setting_key, 'color' ); | |
| 187 | 250 | } |
| 188 | 251 | |
| 189 | 252 | /** |
| 190 | 253 | * Get color markup from color setting value |
| 191 | 254 | * |
| 255 | + * @since 2.05 | |
| 256 | + * | |
| 192 | 257 | * @param string $color_markup value of a color setting, with format #FFFFF, FFFFFF, or white. |
| 193 | 258 | * |
| 194 | - * @since 2.05 | |
| 195 | - * | |
| 196 | 259 | * @return string |
| 197 | 260 | */ |
| 198 | 261 | private function get_color_markup( $color_markup ) { |
| 199 | 262 | $color_markup = trim( $color_markup ); |
| @@ -199,9 +262,9 @@ | ||
| 199 | 262 | $color_markup = trim( $color_markup ); |
| 200 | 263 | |
| 201 | 264 | // Check if each character in string is valid hex digit |
| 202 | 265 | if ( FrmAppHelper::ctype_xdigit( $color_markup ) ) { |
| 203 | - $color_markup = '#' . $color_markup; | |
| 266 | + return '#' . $color_markup; | |
| 204 | 267 | } |
| 205 | 268 | |
| 206 | 269 | return $color_markup; |
| 207 | 270 | } |
| @@ -213,9 +276,9 @@ | ||
| 213 | 276 | * |
| 214 | 277 | * @return string |
| 215 | 278 | */ |
| 216 | 279 | private function table_row_background_color() { |
| 217 | - return ( $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color'] ); | |
| 280 | + return $this->odd ? $this->style_settings['bg_color'] : $this->style_settings['alt_bg_color']; | |
| 218 | 281 | } |
| 219 | 282 | |
| 220 | 283 | /** |
| 221 | 284 | * Get the table row style |
| @@ -221,13 +284,13 @@ | ||
| 221 | 284 | * Get the table row style |
| 222 | 285 | * |
| 223 | 286 | * @since 2.04 |
| 224 | 287 | * @since 5.0.16 Changed scope from `private` to `protected`. |
| 288 | + * @since 6.25 Changed scope from `protected` to `public`. | |
| 225 | 289 | * |
| 226 | 290 | * @return string |
| 227 | 291 | */ |
| 228 | - protected function tr_style() { | |
| 229 | - | |
| 292 | + public function tr_style() { | |
| 230 | 293 | if ( $this->type === 'shortcode' ) { |
| 231 | 294 | $tr_style = ' style="[frm-alt-color]"'; |
| 232 | 295 | } elseif ( $this->use_inline_style ) { |
| 233 | 296 | $tr_style = ' style="background-color:' . $this->table_row_background_color() . ';"'; |
| @@ -238,8 +301,30 @@ | ||
| 238 | 301 | return $tr_style; |
| 239 | 302 | } |
| 240 | 303 | |
| 241 | 304 | /** |
| 305 | + * Gets table style. | |
| 306 | + * | |
| 307 | + * @since 6.25 | |
| 308 | + * | |
| 309 | + * @return string | |
| 310 | + */ | |
| 311 | + public function get_table_style() { | |
| 312 | + return $this->table_style; | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * Gets td style. | |
| 317 | + * | |
| 318 | + * @since 6.25 | |
| 319 | + * | |
| 320 | + * @return string | |
| 321 | + */ | |
| 322 | + public function get_td_style() { | |
| 323 | + return $this->td_style; | |
| 324 | + } | |
| 325 | + | |
| 326 | + /** | |
| 242 | 327 | * Switch the odd property from true to false or false to true |
| 243 | 328 | * |
| 244 | 329 | * @since 2.04 |
| 245 | 330 | * @since 5.0.16 Changed scope from `private` to `protected`. |
| @@ -259,9 +344,9 @@ | ||
| 259 | 344 | * |
| 260 | 345 | * @return string |
| 261 | 346 | */ |
| 262 | 347 | public function generate_table_header() { |
| 263 | - return '<table cellspacing="0" ' . $this->table_style . '><tbody>' . "\r\n"; | |
| 348 | + return '<table cellspacing="0"' . $this->table_style . '><tbody>' . "\r\n"; | |
| 264 | 349 | } |
| 265 | 350 | |
| 266 | 351 | /** |
| 267 | 352 | * Generate a table footer |
| @@ -280,20 +365,20 @@ | ||
| 280 | 365 | * @since 2.04 |
| 281 | 366 | * |
| 282 | 367 | * @param string $label The label. |
| 283 | 368 | * @param string $value The value. |
| 369 | + * @param array $args Additional arguments. May include "field_type" string. | |
| 284 | 370 | * |
| 285 | 371 | * @return string |
| 286 | 372 | */ |
| 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 .= '>'; | |
| 373 | + public function generate_two_cell_table_row( $label, $value, $args = array() ) { | |
| 374 | + $row = '<tr' . $this->tr_style(); | |
| 375 | + $row .= $this->add_row_class( $value === '' ); | |
| 376 | + $row .= '>'; | |
| 377 | + $label = '<th scope="row"' . $this->td_style . '>' . wp_kses_post( $label ) . '</th>'; | |
| 378 | + $value = '<td' . $this->td_style . '>' . $this->filter_value_for_display( $value, $args ) . '</td>'; | |
| 291 | 379 | |
| 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 ) { | |
| 380 | + if ( 'rtl' === $this->direction ) { | |
| 296 | 381 | $row .= $value; |
| 297 | 382 | $row .= $label; |
| 298 | 383 | } else { |
| 299 | 384 | $row .= $label; |
| @@ -307,8 +392,23 @@ | ||
| 307 | 392 | return $row; |
| 308 | 393 | } |
| 309 | 394 | |
| 310 | 395 | /** |
| 396 | + * @param string $value | |
| 397 | + * @param array $args | |
| 398 | + * | |
| 399 | + * @return string | |
| 400 | + */ | |
| 401 | + private function filter_value_for_display( $value, $args ) { | |
| 402 | + if ( empty( $args['field_type'] ) ) { | |
| 403 | + return wp_kses_post( $value ); | |
| 404 | + } | |
| 405 | + | |
| 406 | + $field_object = FrmFieldFactory::get_field_type( $args['field_type'] ); | |
| 407 | + return $field_object->filter_value_for_table_html( $value ); | |
| 408 | + } | |
| 409 | + | |
| 410 | + /** | |
| 311 | 411 | * Generate a single cell row for an HTML table |
| 312 | 412 | * |
| 313 | 413 | * @since 2.04 |
| 314 | 414 | * |
| @@ -316,9 +416,9 @@ | ||
| 316 | 416 | * |
| 317 | 417 | * @return string |
| 318 | 418 | */ |
| 319 | 419 | public function generate_single_cell_table_row( $value ) { |
| 320 | - $row = '<tr' . $this->tr_style(); | |
| 420 | + $row = '<tr' . $this->tr_style(); | |
| 321 | 421 | $row .= $this->add_row_class(); |
| 322 | 422 | $row .= '>'; |
| 323 | 423 | $row .= '<td colspan="2"' . $this->td_style . '>' . $value . '</td>'; |
| 324 | 424 | $row .= '</tr>' . "\r\n"; |
| @@ -338,17 +438,17 @@ | ||
| 338 | 438 | * @return string |
| 339 | 439 | */ |
| 340 | 440 | protected function add_row_class( $empty = false ) { |
| 341 | 441 | $class = ''; |
| 442 | + | |
| 342 | 443 | if ( $empty ) { |
| 343 | 444 | // Only add this class on two cell rows. |
| 344 | 445 | $class .= ' frm-empty-row'; |
| 345 | 446 | } |
| 447 | + | |
| 346 | 448 | if ( $this->is_child ) { |
| 347 | 449 | $class .= ' frm-child-row'; |
| 348 | 450 | } |
| 349 | - if ( $class ) { | |
| 350 | - $class = ' class="' . trim( $class ) . '"'; | |
| 351 | - } | |
| 352 | - return $class; | |
| 451 | + | |
| 452 | + return $class ? ' class="' . trim( $class ) . '"' : $class; | |
| 353 | 453 | } |
| 354 | 454 | } |