| @@ -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 = ''; |
| @@ -74,8 +81,9 @@ | ||
| 74 | 81 | /** |
| 75 | 82 | * Used to add a class in tables. Set in Pro. |
| 76 | 83 | * |
| 77 | 84 | * @var bool |
| 85 | + * | |
| 78 | 86 | * @since 5.4.2 |
| 79 | 87 | */ |
| 80 | 88 | public $is_child = false; |
| 81 | 89 | |
| @@ -85,11 +93,10 @@ | ||
| 85 | 93 | * @param string $type |
| 86 | 94 | * @param array $atts |
| 87 | 95 | */ |
| 88 | 96 | public function __construct( $type, $atts = array() ) { |
| 97 | + $this->type = $type; | |
| 89 | 98 | |
| 90 | - $this->type = (string) $type; | |
| 91 | - | |
| 92 | 99 | if ( isset( $atts['cell_padding'] ) ) { |
| 93 | 100 | $this->cell_padding = $atts['cell_padding']; |
| 94 | 101 | } |
| 95 | 102 | |
| @@ -109,8 +116,9 @@ | ||
| 109 | 116 | * |
| 110 | 117 | * @since 2.04 |
| 111 | 118 | * |
| 112 | 119 | * @param array $atts |
| 120 | + * | |
| 113 | 121 | * @return void |
| 114 | 122 | */ |
| 115 | 123 | private function init_style_settings( $atts ) { |
| 116 | 124 | $style_settings = array( |
| @@ -141,8 +149,9 @@ | ||
| 141 | 149 | * |
| 142 | 150 | * @since 2.04 |
| 143 | 151 | * |
| 144 | 152 | * @param array $atts |
| 153 | + * | |
| 145 | 154 | * @return void |
| 146 | 155 | */ |
| 147 | 156 | private function init_use_inline_style( $atts ) { |
| 148 | 157 | if ( isset( $atts['inline_style'] ) && ! $atts['inline_style'] ) { |
| @@ -155,8 +164,9 @@ | ||
| 155 | 164 | * |
| 156 | 165 | * @since 2.04 |
| 157 | 166 | * |
| 158 | 167 | * @param array $atts |
| 168 | + * | |
| 159 | 169 | * @return void |
| 160 | 170 | */ |
| 161 | 171 | private function init_direction( $atts ) { |
| 162 | 172 | if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) { |
| @@ -167,16 +177,15 @@ | ||
| 167 | 177 | /** |
| 168 | 178 | * Set the table_style property |
| 169 | 179 | * |
| 170 | 180 | * @since 2.04 |
| 181 | + * | |
| 171 | 182 | * @return void |
| 172 | 183 | */ |
| 173 | 184 | private function init_table_style() { |
| 174 | 185 | if ( $this->use_inline_style === true ) { |
| 175 | - | |
| 176 | 186 | $this->table_style = ' style="' . esc_attr( 'border-spacing:0;font-size:' . $this->style_settings['font_size'] . ';line-height:135%;' ); |
| 177 | 187 | $this->table_style .= esc_attr( 'border-bottom:' . $this->style_settings['border_width'] . ' solid ' . $this->style_settings['border_color'] . ';' ) . '"'; |
| 178 | - | |
| 179 | 188 | } |
| 180 | 189 | |
| 181 | 190 | if ( ! empty( $this->style_settings['class'] ) ) { |
| 182 | 191 | $this->table_style .= ' class="' . esc_attr( $this->style_settings['class'] ) . '"'; |
| @@ -181,9 +190,9 @@ | ||
| 181 | 190 | if ( ! empty( $this->style_settings['class'] ) ) { |
| 182 | 191 | $this->table_style .= ' class="' . esc_attr( $this->style_settings['class'] ) . '"'; |
| 183 | 192 | } |
| 184 | 193 | |
| 185 | - if ( ! empty( $this->width ) ) { | |
| 194 | + if ( $this->width ) { | |
| 186 | 195 | $this->table_style .= ' width="' . esc_attr( $this->width ) . '"'; |
| 187 | 196 | } |
| 188 | 197 | } |
| 189 | 198 | |
| @@ -190,19 +199,21 @@ | ||
| 190 | 199 | /** |
| 191 | 200 | * Set the td_style property |
| 192 | 201 | * |
| 193 | 202 | * @since 2.04 |
| 203 | + * | |
| 194 | 204 | * @return void |
| 195 | 205 | */ |
| 196 | 206 | private function init_td_style() { |
| 197 | - if ( $this->use_inline_style === true ) { | |
| 207 | + if ( $this->use_inline_style !== true ) { | |
| 208 | + return; | |
| 209 | + } | |
| 198 | 210 | |
| 199 | - $td_style_attributes = 'text-align:' . ( $this->direction === 'rtl' ? 'right' : 'left' ) . ';'; | |
| 200 | - $td_style_attributes .= 'color:' . $this->style_settings['text_color'] . ';padding:' . $this->cell_padding . ';vertical-align:top;'; | |
| 201 | - $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'] . ';'; | |
| 202 | 214 | |
| 203 | - $this->td_style = ' style="' . esc_attr( $td_style_attributes ) . '"'; | |
| 204 | - } | |
| 215 | + $this->td_style = ' style="' . esc_attr( $td_style_attributes ) . '"'; | |
| 205 | 216 | } |
| 206 | 217 | |
| 207 | 218 | /** |
| 208 | 219 | * Removes border CSS from HTML. |
| @@ -210,8 +221,9 @@ | ||
| 210 | 221 | * @since 6.25 |
| 211 | 222 | * |
| 212 | 223 | * @param string $html The HTML. |
| 213 | 224 | * @param string $position The border position. Default is `top`. |
| 225 | + * | |
| 214 | 226 | * @return string |
| 215 | 227 | */ |
| 216 | 228 | public function remove_border( $html, $position = 'top' ) { |
| 217 | 229 | $search = sprintf( |
| @@ -227,14 +239,15 @@ | ||
| 227 | 239 | /** |
| 228 | 240 | * Determine if setting is for a color, e.g. text color, background color, or border color |
| 229 | 241 | * |
| 230 | 242 | * @since 2.05 |
| 243 | + * | |
| 231 | 244 | * @param string $setting_key Name of setting. |
| 232 | 245 | * |
| 233 | 246 | * @return bool |
| 234 | 247 | */ |
| 235 | 248 | private function is_color_setting( $setting_key ) { |
| 236 | - return strpos( $setting_key, 'color' ) !== false; | |
| 249 | + return str_contains( $setting_key, 'color' ); | |
| 237 | 250 | } |
| 238 | 251 | |
| 239 | 252 | /** |
| 240 | 253 | * Get color markup from color setting value |
| @@ -239,8 +252,9 @@ | ||
| 239 | 252 | /** |
| 240 | 253 | * Get color markup from color setting value |
| 241 | 254 | * |
| 242 | 255 | * @since 2.05 |
| 256 | + * | |
| 243 | 257 | * @param string $color_markup value of a color setting, with format #FFFFF, FFFFFF, or white. |
| 244 | 258 | * |
| 245 | 259 | * @return string |
| 246 | 260 | */ |
| @@ -248,9 +262,9 @@ | ||
| 248 | 262 | $color_markup = trim( $color_markup ); |
| 249 | 263 | |
| 250 | 264 | // Check if each character in string is valid hex digit |
| 251 | 265 | if ( FrmAppHelper::ctype_xdigit( $color_markup ) ) { |
| 252 | - $color_markup = '#' . $color_markup; | |
| 266 | + return '#' . $color_markup; | |
| 253 | 267 | } |
| 254 | 268 | |
| 255 | 269 | return $color_markup; |
| 256 | 270 | } |
| @@ -262,9 +276,9 @@ | ||
| 262 | 276 | * |
| 263 | 277 | * @return string |
| 264 | 278 | */ |
| 265 | 279 | private function table_row_background_color() { |
| 266 | - 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']; | |
| 267 | 281 | } |
| 268 | 282 | |
| 269 | 283 | /** |
| 270 | 284 | * Get the table row style |
| @@ -275,9 +289,8 @@ | ||
| 275 | 289 | * |
| 276 | 290 | * @return string |
| 277 | 291 | */ |
| 278 | 292 | public function tr_style() { |
| 279 | - | |
| 280 | 293 | if ( $this->type === 'shortcode' ) { |
| 281 | 294 | $tr_style = ' style="[frm-alt-color]"'; |
| 282 | 295 | } elseif ( $this->use_inline_style ) { |
| 283 | 296 | $tr_style = ' style="background-color:' . $this->table_row_background_color() . ';"'; |
| @@ -352,20 +365,20 @@ | ||
| 352 | 365 | * @since 2.04 |
| 353 | 366 | * |
| 354 | 367 | * @param string $label The label. |
| 355 | 368 | * @param string $value The value. |
| 369 | + * @param array $args Additional arguments. May include "field_type" string. | |
| 356 | 370 | * |
| 357 | 371 | * @return string |
| 358 | 372 | */ |
| 359 | - public function generate_two_cell_table_row( $label, $value ) { | |
| 360 | - $row = '<tr' . $this->tr_style(); | |
| 361 | - $row .= $this->add_row_class( $value === '' ); | |
| 362 | - $row .= '>'; | |
| 363 | - | |
| 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 .= '>'; | |
| 364 | 377 | $label = '<th scope="row"' . $this->td_style . '>' . wp_kses_post( $label ) . '</th>'; |
| 365 | - $value = '<td' . $this->td_style . '>' . wp_kses_post( $value ) . '</td>'; | |
| 378 | + $value = '<td' . $this->td_style . '>' . $this->filter_value_for_display( $value, $args ) . '</td>'; | |
| 366 | 379 | |
| 367 | - if ( 'rtl' == $this->direction ) { | |
| 380 | + if ( 'rtl' === $this->direction ) { | |
| 368 | 381 | $row .= $value; |
| 369 | 382 | $row .= $label; |
| 370 | 383 | } else { |
| 371 | 384 | $row .= $label; |
| @@ -379,8 +392,23 @@ | ||
| 379 | 392 | return $row; |
| 380 | 393 | } |
| 381 | 394 | |
| 382 | 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 | + /** | |
| 383 | 411 | * Generate a single cell row for an HTML table |
| 384 | 412 | * |
| 385 | 413 | * @since 2.04 |
| 386 | 414 | * |
| @@ -410,17 +438,17 @@ | ||
| 410 | 438 | * @return string |
| 411 | 439 | */ |
| 412 | 440 | protected function add_row_class( $empty = false ) { |
| 413 | 441 | $class = ''; |
| 442 | + | |
| 414 | 443 | if ( $empty ) { |
| 415 | 444 | // Only add this class on two cell rows. |
| 416 | 445 | $class .= ' frm-empty-row'; |
| 417 | 446 | } |
| 447 | + | |
| 418 | 448 | if ( $this->is_child ) { |
| 419 | 449 | $class .= ' frm-child-row'; |
| 420 | 450 | } |
| 421 | - if ( $class ) { | |
| 422 | - $class = ' class="' . trim( $class ) . '"'; | |
| 423 | - } | |
| 424 | - return $class; | |
| 451 | + | |
| 452 | + return $class ? ' class="' . trim( $class ) . '"' : $class; | |
| 425 | 453 | } |
| 426 | 454 | } |