| 1 |
<?php |
| 2 |
namespace ABlocks\Blocks\Table; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Classes\BlockBaseAbstract; |
| 9 |
use ABlocks\Classes\CssGenerator; |
| 10 |
use ABlocks\Controls\Border; |
| 11 |
use ABlocks\Classes\CssGeneratorV2; |
| 12 |
use ABlocks\Controls\Color; |
| 13 |
|
| 14 |
class Block extends BlockBaseAbstract { |
| 15 |
protected $block_name = 'table'; |
| 16 |
|
| 17 |
public function build_css_v1( $attributes ) { |
| 18 |
$css_generator = new CssGenerator( $attributes, $this->block_name ); |
| 19 |
$css_generator->add_class_styles( |
| 20 |
'{{WRAPPER}}.ablocks-block--table table', |
| 21 |
$this->get_table_css( $attributes ) |
| 22 |
); |
| 23 |
$css_generator->add_class_styles( |
| 24 |
'{{WRAPPER}}.ablocks-block--table table, {{WRAPPER}}.ablocks-block--table-header .ablocks-block--table-cell , {{WRAPPER}}.ablocks-block--table .ablocks-block--table-cell', |
| 25 |
$this->get_table_border_css( $attributes ), |
| 26 |
$this->get_table_border_css( $attributes, 'Tablet' ), |
| 27 |
$this->get_table_border_css( $attributes, 'Mobile' ) |
| 28 |
); |
| 29 |
$css_generator->add_class_styles( |
| 30 |
'{{WRAPPER}}.ablocks-block--table table:hover, {{WRAPPER}}.ablocks-block--table-header .ablocks-block--table-cell:hover, {{WRAPPER}}.ablocks-block--table .ablocks-block--table-cell:hover', |
| 31 |
$this->get_table_border_hover_css( $attributes ), |
| 32 |
$this->get_table_border_hover_css( $attributes, 'Tablet' ), |
| 33 |
$this->get_table_border_hover_css( $attributes, 'Mobile' ) |
| 34 |
); |
| 35 |
$css_generator->add_class_styles( |
| 36 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--odd', |
| 37 |
$this->get_row_odd_css( $attributes ) |
| 38 |
); |
| 39 |
$css_generator->add_class_styles( |
| 40 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--odd:hover', |
| 41 |
$this->get_row_odd_hover_css( $attributes ) |
| 42 |
); |
| 43 |
$css_generator->add_class_styles( |
| 44 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--even', |
| 45 |
$this->get_row_even_css( $attributes ) |
| 46 |
); |
| 47 |
$css_generator->add_class_styles( |
| 48 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--even:hover', |
| 49 |
$this->get_row_even_hover_css( $attributes ) |
| 50 |
); |
| 51 |
// ---header---- |
| 52 |
$css_generator->add_class_styles( |
| 53 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-header', |
| 54 |
$this->get_header_css( $attributes ) |
| 55 |
); |
| 56 |
$css_generator->add_class_styles( |
| 57 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-header:hover', |
| 58 |
$this->get_header_hover_css( $attributes ) |
| 59 |
); |
| 60 |
// --table body---- |
| 61 |
$css_generator->add_class_styles( |
| 62 |
'{{WRAPPER}} .ablocks-block--table-body', |
| 63 |
$this->get_body_css( $attributes ) |
| 64 |
); |
| 65 |
$css_generator->add_class_styles( |
| 66 |
'{{WRAPPER}} .ablocks-block--table-body:hover', |
| 67 |
$this->get_body_hover_css( $attributes ) |
| 68 |
); |
| 69 |
|
| 70 |
// --table footer-- |
| 71 |
$css_generator->add_class_styles( |
| 72 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-footer', |
| 73 |
$this->get_footer_css( $attributes ) |
| 74 |
); |
| 75 |
$css_generator->add_class_styles( |
| 76 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-footer:hover', |
| 77 |
$this->get_footer_hover_css( $attributes ) |
| 78 |
); |
| 79 |
return $css_generator->generate_css(); |
| 80 |
} |
| 81 |
public function build_css_v2( $attributes ) { |
| 82 |
$css_generator = new CssGeneratorV2( $attributes, $this->block_name ); |
| 83 |
$css_generator->add_class_styles( |
| 84 |
'{{WRAPPER}}.ablocks-block--table table', |
| 85 |
$this->get_table_css( $attributes ) |
| 86 |
); |
| 87 |
$css_generator->add_class_styles( |
| 88 |
'{{WRAPPER}}.ablocks-block--table table, {{WRAPPER}}.ablocks-block--table-header .ablocks-block--table-cell , {{WRAPPER}}.ablocks-block--table .ablocks-block--table-cell', |
| 89 |
$this->get_table_border_css( $attributes ), |
| 90 |
$this->get_table_border_css( $attributes, 'Tablet' ), |
| 91 |
$this->get_table_border_css( $attributes, 'Mobile' ) |
| 92 |
); |
| 93 |
$css_generator->add_class_styles( |
| 94 |
'{{WRAPPER}}.ablocks-block--table table:hover, {{WRAPPER}}.ablocks-block--table-header .ablocks-block--table-cell:hover, {{WRAPPER}}.ablocks-block--table .ablocks-block--table-cell:hover', |
| 95 |
$this->get_table_border_hover_css( $attributes ), |
| 96 |
$this->get_table_border_hover_css( $attributes, 'Tablet' ), |
| 97 |
$this->get_table_border_hover_css( $attributes, 'Mobile' ) |
| 98 |
); |
| 99 |
$css_generator->add_class_styles( |
| 100 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--odd', |
| 101 |
$this->get_row_odd_css( $attributes ) |
| 102 |
); |
| 103 |
$css_generator->add_class_styles( |
| 104 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--odd:hover', |
| 105 |
$this->get_row_odd_hover_css( $attributes ) |
| 106 |
); |
| 107 |
$css_generator->add_class_styles( |
| 108 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--even', |
| 109 |
$this->get_row_even_css( $attributes ) |
| 110 |
); |
| 111 |
$css_generator->add_class_styles( |
| 112 |
'{{WRAPPER}} .ablocks-block--table-body .ablocks-table-row--even:hover', |
| 113 |
$this->get_row_even_hover_css( $attributes ) |
| 114 |
); |
| 115 |
// ---header---- |
| 116 |
$css_generator->add_class_styles( |
| 117 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-header', |
| 118 |
$this->get_header_css( $attributes ) |
| 119 |
); |
| 120 |
$css_generator->add_class_styles( |
| 121 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-header:hover', |
| 122 |
$this->get_header_hover_css( $attributes ) |
| 123 |
); |
| 124 |
// --table body---- |
| 125 |
$css_generator->add_class_styles( |
| 126 |
'{{WRAPPER}} .ablocks-block--table-body', |
| 127 |
$this->get_body_css( $attributes ) |
| 128 |
); |
| 129 |
$css_generator->add_class_styles( |
| 130 |
'{{WRAPPER}} .ablocks-block--table-body:hover', |
| 131 |
$this->get_body_hover_css( $attributes ) |
| 132 |
); |
| 133 |
|
| 134 |
// --table footer-- |
| 135 |
$css_generator->add_class_styles( |
| 136 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-footer', |
| 137 |
$this->get_footer_css( $attributes ) |
| 138 |
); |
| 139 |
$css_generator->add_class_styles( |
| 140 |
'{{WRAPPER}}.ablocks-block--table .ablocks-block--table-footer:hover', |
| 141 |
$this->get_footer_hover_css( $attributes ) |
| 142 |
); |
| 143 |
return $css_generator->generate_css(); |
| 144 |
} |
| 145 |
public function build_css( $attributes ) { |
| 146 |
if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { |
| 147 |
return $this->build_css_v2( $attributes ); |
| 148 |
} |
| 149 |
return $this->build_css_v1( $attributes ); |
| 150 |
} |
| 151 |
public function get_table_css( $attributes, $device = '' ) { |
| 152 |
$css = []; |
| 153 |
|
| 154 |
if ( ! empty( $attributes['borderCollapse'] ) && $attributes['borderCollapse'] === 'collapse' ) { |
| 155 |
$css['border-collapse'] = 'collapse'; |
| 156 |
} elseif ( ! empty( $attributes['borderCollapse'] ) && $attributes['borderCollapse'] === 'separate' ) { |
| 157 |
$css['border-collapse'] = 'separate'; |
| 158 |
} |
| 159 |
|
| 160 |
return $css; |
| 161 |
} |
| 162 |
|
| 163 |
public function get_table_border_css( $attributes, $device = '' ) { |
| 164 |
|
| 165 |
return Border::get_css( $attributes['border'], '', $device ); |
| 166 |
|
| 167 |
} |
| 168 |
public function get_table_border_hover_css( $attributes, $device = '' ) { |
| 169 |
|
| 170 |
return Border::get_hover_css( $attributes['border'], '', $device ); |
| 171 |
} |
| 172 |
|
| 173 |
public function get_row_odd_css( $attributes, $device = '' ) { |
| 174 |
return [ 'background' => Color::get_css( isset( $attributes['rowOddColor'] ) ? $attributes['rowOddColor'] : '' ) ]; |
| 175 |
} |
| 176 |
|
| 177 |
public function get_row_odd_hover_css( $attributes, $device = '' ) { |
| 178 |
return [ 'background' => Color::get_css( isset( $attributes['rowOddColorH'] ) ? $attributes['rowOddColorH'] : '' ) ]; |
| 179 |
} |
| 180 |
|
| 181 |
public function get_row_even_css( $attributes, $device = '' ) { |
| 182 |
return [ 'background' => Color::get_css( isset( $attributes['rowEvenColor'] ) ? $attributes['rowEvenColor'] : '' ) ]; |
| 183 |
} |
| 184 |
|
| 185 |
public function get_row_even_hover_css( $attributes, $device = '' ) { |
| 186 |
return [ 'background' => Color::get_css( isset( $attributes['rowEvenColorH'] ) ? $attributes['rowEvenColorH'] : '' ) ]; |
| 187 |
} |
| 188 |
|
| 189 |
public function get_header_css( $attributes, $device = '' ) { |
| 190 |
$css = []; |
| 191 |
$css['background'] = Color::get_css( |
| 192 |
isset( $attributes['headerColor'] ) ? $attributes['headerColor'] : '') . ' !important'; |
| 193 |
return $css; |
| 194 |
} |
| 195 |
|
| 196 |
public function get_header_hover_css( $attributes, $device = '' ) { |
| 197 |
$css = []; |
| 198 |
$css['background'] = Color::get_css( |
| 199 |
isset( $attributes['headerColorH'] ) ? $attributes['headerColorH'] : '') . ' !important'; |
| 200 |
|
| 201 |
return $css; |
| 202 |
} |
| 203 |
|
| 204 |
public function get_body_css( $attributes, $device = '' ) { |
| 205 |
$css = []; |
| 206 |
$css['background'] = Color::get_css( |
| 207 |
isset( $attributes['bodyBg'] ) ? $attributes['bodyBg'] : '') . ' !important'; |
| 208 |
return $css; |
| 209 |
} |
| 210 |
|
| 211 |
public function get_body_hover_css( $attributes, $device = '' ) { |
| 212 |
$css = []; |
| 213 |
$css['background'] = Color::get_css( |
| 214 |
isset( $attributes['bodyBgH'] ) ? $attributes['bodyBgH'] : '') . ' !important'; |
| 215 |
|
| 216 |
return $css; |
| 217 |
} |
| 218 |
|
| 219 |
public function get_footer_css( $attributes, $device = '' ) { |
| 220 |
$css = []; |
| 221 |
$css['background'] = Color::get_css( |
| 222 |
isset( $attributes['footerColor'] ) ? $attributes['footerColor'] : '') . ' !important'; |
| 223 |
|
| 224 |
return $css; |
| 225 |
} |
| 226 |
|
| 227 |
public function get_footer_hover_css( $attributes, $device = '' ) { |
| 228 |
$css = []; |
| 229 |
|
| 230 |
$css['background'] = Color::get_css( |
| 231 |
isset( $attributes['footerColorH'] ) ? $attributes['footerColorH'] : '') . ' !important'; |
| 232 |
|
| 233 |
return $css; |
| 234 |
} |
| 235 |
} |
| 236 |
|