| @@ -1,0 +1,343 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\Accordion; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\BlockBaseAbstract; | |
| 9 | +use ABlocks\Classes\CssGenerator; | |
| 10 | +use ABlocks\Classes\CssGeneratorV2; | |
| 11 | +use ABlocks\Helper; | |
| 12 | +use ABlocks\Controls\Typography; | |
| 13 | +use ABlocks\Controls\TextShadow; | |
| 14 | +use ABlocks\Controls\TextStroke; | |
| 15 | +use ABlocks\Controls\Background; | |
| 16 | +use ABlocks\Controls\Border; | |
| 17 | +use ABlocks\Controls\Dimensions; | |
| 18 | +use ABlocks\Controls\Alignment; | |
| 19 | +use ABlocks\Controls\Range; | |
| 20 | +use ABlocks\Controls\Color; | |
| 21 | + | |
| 22 | +class Block extends BlockBaseAbstract { | |
| 23 | + protected $block_name = 'accordion'; | |
| 24 | + | |
| 25 | + public function render_callback( $attributes, $content, $block_instance ) { | |
| 26 | + $content = parent::render_callback( $attributes, $content, $block_instance ); | |
| 27 | + if ( ! empty( $attributes['enableFaqSchema'] ) ) { | |
| 28 | + static $faq_schema_rendered = false; | |
| 29 | + if ( ! $faq_schema_rendered ) { | |
| 30 | + $schema = $this->generate_faq_schema( $block_instance ); | |
| 31 | + if ( $schema ) { | |
| 32 | + $content .= '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . '</script>'; | |
| 33 | + $faq_schema_rendered = true; | |
| 34 | + } | |
| 35 | + } | |
| 36 | + } | |
| 37 | + return $content; | |
| 38 | + } | |
| 39 | + | |
| 40 | + private function generate_faq_schema( $block_instance ) { | |
| 41 | + $items = []; | |
| 42 | + foreach ( $block_instance->inner_blocks as $inner_block ) { | |
| 43 | + if ( $inner_block->name !== 'ablocks/single-accordion' ) { | |
| 44 | + continue; | |
| 45 | + } | |
| 46 | + $question = isset( $inner_block->attributes['accordionTitle'] ) ? wp_strip_all_tags( $inner_block->attributes['accordionTitle'] ) : ''; | |
| 47 | + $answer_parts = []; | |
| 48 | + foreach ( $inner_block->inner_blocks as $child ) { | |
| 49 | + $answer_parts[] = wp_strip_all_tags( render_block( $child->parsed_block ) ); | |
| 50 | + } | |
| 51 | + $answer = trim( preg_replace( '/\s+/', ' ', implode( ' ', $answer_parts ) ) ); | |
| 52 | + if ( $question && $answer ) { | |
| 53 | + $items[] = [ | |
| 54 | + '@type' => 'Question', | |
| 55 | + 'name' => $question, | |
| 56 | + 'acceptedAnswer' => [ '@type' => 'Answer', 'text' => $answer ], | |
| 57 | + ]; | |
| 58 | + } | |
| 59 | + } | |
| 60 | + if ( empty( $items ) ) { | |
| 61 | + return null; | |
| 62 | + } | |
| 63 | + return [ | |
| 64 | + '@context' => 'https://schema.org', | |
| 65 | + '@type' => 'FAQPage', | |
| 66 | + 'mainEntity' => $items, | |
| 67 | + ]; | |
| 68 | + } | |
| 69 | + | |
| 70 | + public function build_css_v1( $attributes ) { | |
| 71 | + $css_generator = new CssGenerator( $attributes ); | |
| 72 | + | |
| 73 | + $css_generator->add_class_styles( | |
| 74 | + '{{WRAPPER}} .ablocks-block--single-accordion', | |
| 75 | + $this->get_item_css( $attributes ), | |
| 76 | + $this->get_item_css( $attributes, 'Tablet' ), | |
| 77 | + $this->get_item_css( $attributes, 'Mobile' ), | |
| 78 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_item_css( $attributes, $device ); } ) | |
| 79 | + ); | |
| 80 | + $css_generator->add_class_styles( | |
| 81 | + '{{WRAPPER}} .ablocks-block--single-accordion:hover', | |
| 82 | + $this->get_item_hover_css( $attributes ), | |
| 83 | + $this->get_item_hover_css( $attributes, 'Tablet' ), | |
| 84 | + $this->get_item_hover_css( $attributes, 'Mobile' ), | |
| 85 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_item_hover_css( $attributes, $device ); } ) | |
| 86 | + ); | |
| 87 | + $css_generator->add_class_styles( | |
| 88 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading .ablocks-block-accordion-title', | |
| 89 | + $this->get_title_css( $attributes ), | |
| 90 | + $this->get_title_css( $attributes, 'Tablet' ), | |
| 91 | + $this->get_title_css( $attributes, 'Mobile' ), | |
| 92 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_title_css( $attributes, $device ); } ) | |
| 93 | + ); | |
| 94 | + $css_generator->add_class_styles( | |
| 95 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading:hover .ablocks-block-accordion-title', | |
| 96 | + $this->get_title_hover_css( $attributes ), | |
| 97 | + $this->get_title_hover_css( $attributes, 'Tablet' ), | |
| 98 | + $this->get_title_hover_css( $attributes, 'Mobile' ), | |
| 99 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_title_hover_css( $attributes, $device ); } ) | |
| 100 | + ); | |
| 101 | + $css_generator->add_class_styles( | |
| 102 | + '{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block-accordion-title', | |
| 103 | + $this->get_title_active_css( $attributes ), | |
| 104 | + $this->get_title_active_css( $attributes, 'Tablet' ), | |
| 105 | + $this->get_title_active_css( $attributes, 'Mobile' ), | |
| 106 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_title_active_css( $attributes, $device ); } ) | |
| 107 | + ); | |
| 108 | + $css_generator->add_class_styles( | |
| 109 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading', | |
| 110 | + $this->get_panel_css( $attributes ), | |
| 111 | + $this->get_panel_css( $attributes, 'Tablet' ), | |
| 112 | + $this->get_panel_css( $attributes, 'Mobile' ), | |
| 113 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_panel_css( $attributes, $device ); } ) | |
| 114 | + ); | |
| 115 | + $css_generator->add_class_styles( | |
| 116 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading:hover', | |
| 117 | + $this->get_panel_hover_css( $attributes ), | |
| 118 | + $this->get_panel_hover_css( $attributes, 'Tablet' ), | |
| 119 | + $this->get_panel_hover_css( $attributes, 'Mobile' ), | |
| 120 | + ); | |
| 121 | + $css_generator->add_class_styles( | |
| 122 | + '{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block--single-accordion__heading', | |
| 123 | + $this->get_panel_active_css( $attributes ), | |
| 124 | + $this->get_panel_active_css( $attributes, 'Tablet' ), | |
| 125 | + $this->get_panel_active_css( $attributes, 'Mobile' ), | |
| 126 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_panel_active_css( $attributes, $device ); } ) | |
| 127 | + ); | |
| 128 | + $css_generator->add_class_styles( | |
| 129 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading svg.ablocks-svg-icon', | |
| 130 | + $this->get_icon_css( $attributes ) | |
| 131 | + ); | |
| 132 | + $css_generator->add_class_styles( | |
| 133 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading:hover svg.ablocks-svg-icon', | |
| 134 | + $this->get_icon_hover_css( $attributes ) | |
| 135 | + ); | |
| 136 | + $css_generator->add_class_styles( | |
| 137 | + '{{WRAPPER}} .ablocks-block--single-accordion-is-selected svg.ablocks-svg-icon', | |
| 138 | + $this->get_icon_active_css( $attributes ) | |
| 139 | + ); | |
| 140 | + $css_generator->add_class_styles( | |
| 141 | + '{{WRAPPER}} .ablocks-block--single-accordion__body-content', | |
| 142 | + $this->get_content_css( $attributes ), | |
| 143 | + $this->get_content_css( $attributes, 'Tablet' ), | |
| 144 | + $this->get_content_css( $attributes, 'Mobile' ), | |
| 145 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_content_css( $attributes, $device ); } ) | |
| 146 | + ); | |
| 147 | + $css_generator->add_class_styles( | |
| 148 | + '{{WRAPPER}} .ablocks-block--single-accordion__body-content:hover', | |
| 149 | + $this->get_content_hover_css( $attributes ), | |
| 150 | + $this->get_content_hover_css( $attributes, 'Tablet' ), | |
| 151 | + $this->get_content_hover_css( $attributes, 'Mobile' ), | |
| 152 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_content_hover_css( $attributes, $device ); } ) | |
| 153 | + ); | |
| 154 | + return $css_generator->generate_css(); | |
| 155 | + } | |
| 156 | + public function build_css_v2( $attributes ) { | |
| 157 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 158 | + | |
| 159 | + $css_generator->add_class_styles( | |
| 160 | + '{{WRAPPER}} .ablocks-block--single-accordion', | |
| 161 | + $this->get_item_css( $attributes ), | |
| 162 | + $this->get_item_css( $attributes, 'Tablet' ), | |
| 163 | + $this->get_item_css( $attributes, 'Mobile' ), | |
| 164 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_item_css( $attributes, $device ); } ) | |
| 165 | + ); | |
| 166 | + $css_generator->add_class_styles( | |
| 167 | + '{{WRAPPER}} .ablocks-block--single-accordion:hover', | |
| 168 | + $this->get_item_hover_css( $attributes ), | |
| 169 | + $this->get_item_hover_css( $attributes, 'Tablet' ), | |
| 170 | + $this->get_item_hover_css( $attributes, 'Mobile' ), | |
| 171 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_item_hover_css( $attributes, $device ); } ) | |
| 172 | + ); | |
| 173 | + $css_generator->add_class_styles( | |
| 174 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading .ablocks-block-accordion-title', | |
| 175 | + $this->get_title_css( $attributes ), | |
| 176 | + $this->get_title_css( $attributes, 'Tablet' ), | |
| 177 | + $this->get_title_css( $attributes, 'Mobile' ), | |
| 178 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_title_css( $attributes, $device ); } ) | |
| 179 | + ); | |
| 180 | + $css_generator->add_class_styles( | |
| 181 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading:hover .ablocks-block-accordion-title', | |
| 182 | + $this->get_title_hover_css( $attributes ), | |
| 183 | + $this->get_title_hover_css( $attributes, 'Tablet' ), | |
| 184 | + $this->get_title_hover_css( $attributes, 'Mobile' ), | |
| 185 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_title_hover_css( $attributes, $device ); } ) | |
| 186 | + ); | |
| 187 | + $css_generator->add_class_styles( | |
| 188 | + '{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block-accordion-title', | |
| 189 | + $this->get_title_active_css( $attributes ), | |
| 190 | + $this->get_title_active_css( $attributes, 'Tablet' ), | |
| 191 | + $this->get_title_active_css( $attributes, 'Mobile' ), | |
| 192 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_title_active_css( $attributes, $device ); } ) | |
| 193 | + ); | |
| 194 | + $css_generator->add_class_styles( | |
| 195 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading', | |
| 196 | + $this->get_panel_css( $attributes ), | |
| 197 | + $this->get_panel_css( $attributes, 'Tablet' ), | |
| 198 | + $this->get_panel_css( $attributes, 'Mobile' ), | |
| 199 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_panel_css( $attributes, $device ); } ) | |
| 200 | + ); | |
| 201 | + $css_generator->add_class_styles( | |
| 202 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading:hover', | |
| 203 | + $this->get_panel_hover_css( $attributes ), | |
| 204 | + $this->get_panel_hover_css( $attributes, 'Tablet' ), | |
| 205 | + $this->get_panel_hover_css( $attributes, 'Mobile' ), | |
| 206 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_panel_hover_css( $attributes, $device ); } ) | |
| 207 | + ); | |
| 208 | + $css_generator->add_class_styles( | |
| 209 | + '{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block--single-accordion__heading', | |
| 210 | + $this->get_panel_active_css( $attributes ), | |
| 211 | + $this->get_panel_active_css( $attributes, 'Tablet' ), | |
| 212 | + $this->get_panel_active_css( $attributes, 'Mobile' ), | |
| 213 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_panel_active_css( $attributes, $device ); } ) | |
| 214 | + ); | |
| 215 | + $css_generator->add_class_styles( | |
| 216 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading svg.ablocks-svg-icon', | |
| 217 | + $this->get_icon_css( $attributes ) | |
| 218 | + ); | |
| 219 | + $css_generator->add_class_styles( | |
| 220 | + '{{WRAPPER}} .ablocks-block--single-accordion__heading:hover svg.ablocks-svg-icon', | |
| 221 | + $this->get_icon_hover_css( $attributes ) | |
| 222 | + ); | |
| 223 | + $css_generator->add_class_styles( | |
| 224 | + '{{WRAPPER}} .ablocks-block--single-accordion-is-selected svg.ablocks-svg-icon', | |
| 225 | + $this->get_icon_active_css( $attributes ) | |
| 226 | + ); | |
| 227 | + $css_generator->add_class_styles( | |
| 228 | + '{{WRAPPER}} .ablocks-block--single-accordion__body-content', | |
| 229 | + $this->get_content_css( $attributes ), | |
| 230 | + $this->get_content_css( $attributes, 'Tablet' ), | |
| 231 | + $this->get_content_css( $attributes, 'Mobile' ), | |
| 232 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_content_css( $attributes, $device ); } ) | |
| 233 | + ); | |
| 234 | + $css_generator->add_class_styles( | |
| 235 | + '{{WRAPPER}} .ablocks-block--single-accordion__body-content:hover', | |
| 236 | + $this->get_content_hover_css( $attributes ), | |
| 237 | + $this->get_content_hover_css( $attributes, 'Tablet' ), | |
| 238 | + $this->get_content_hover_css( $attributes, 'Mobile' ), | |
| 239 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_content_hover_css( $attributes, $device ); } ) | |
| 240 | + ); | |
| 241 | + return $css_generator->generate_css(); | |
| 242 | + } | |
| 243 | + public function build_css( $attributes ) { | |
| 244 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 245 | + return $this->build_css_v2( $attributes ); | |
| 246 | + } | |
| 247 | + return $this->build_css_v1( $attributes ); | |
| 248 | + } | |
| 249 | + public function get_item_css( $attributes, $device = '' ) { | |
| 250 | + $css = []; | |
| 251 | + | |
| 252 | + return array_merge( | |
| 253 | + Range::get_css([ | |
| 254 | + 'attributeValue' => $attributes['itemSpace'], | |
| 255 | + 'attribute_object_key' => 'value', | |
| 256 | + 'isResponsive' => false, | |
| 257 | + 'defaultValue' => 10, | |
| 258 | + 'property' => 'margin-bottom', | |
| 259 | + 'hasUnit' => false, | |
| 260 | + 'unitDefaultValue' => 'px', | |
| 261 | + 'device' => $device, | |
| 262 | + | |
| 263 | + ]), | |
| 264 | + $css, | |
| 265 | + isset( $attributes['itemBorder'] ) ? Border::get_css( $attributes['itemBorder'], '', $device ) : [] | |
| 266 | + ); | |
| 267 | + } | |
| 268 | + public function get_item_hover_css( $attributes, $device = '' ) { | |
| 269 | + return array_merge( | |
| 270 | + isset( $attributes['itemBorder'] ) ? Border::get_hover_css( $attributes['itemBorder'], '', $device ) : [] | |
| 271 | + ); | |
| 272 | + } | |
| 273 | + public function get_title_css( $attributes, $device = '' ) { | |
| 274 | + $typographyValueGlobal = ! empty( $attributes['headerTypographyGlobal'] ) ? $attributes['headerTypographyGlobal'] : ''; | |
| 275 | + $typography_value = isset( $attributes['headerTypography'] ) ? $attributes['headerTypography'] : []; | |
| 276 | + return array_merge( | |
| 277 | + [ 'color' => Color::get_css( isset( $attributes['headerTextColor'] ) ? $attributes['headerTextColor'] : '' ) ], | |
| 278 | + Range::get_css([ | |
| 279 | + 'attributeValue' => $attributes['iconSpace'], | |
| 280 | + 'attribute_object_key' => 'value', | |
| 281 | + 'isResponsive' => false, | |
| 282 | + 'defaultValue' => 10, | |
| 283 | + 'property' => 'margin-left', | |
| 284 | + 'device' => $device, | |
| 285 | + ]), | |
| 286 | + Typography::get_css( $typography_value, '', $device, $typographyValueGlobal ), | |
| 287 | + TextShadow::get_css( $attributes['headerTextShadow'], '', $device ), | |
| 288 | + TextStroke::get_css( $attributes['headerTextStroke'], '', $device ) | |
| 289 | + ); | |
| 290 | + } | |
| 291 | + public function get_title_hover_css( $attributes, $device = '' ) { | |
| 292 | + return [ 'color' => Color::get_css( isset( $attributes['headerTextColorH'] ) ? $attributes['headerTextColorH'] : '' ) ]; | |
| 293 | + } | |
| 294 | + public function get_title_active_css( $attributes, $device = '' ) { | |
| 295 | + return [ 'color' => Color::get_css( isset( $attributes['headerTextActiveColor'] ) ? $attributes['headerTextActiveColor'] : '' ) ]; | |
| 296 | + } | |
| 297 | + public function get_panel_css( $attributes, $device = '' ) { | |
| 298 | + return array_merge( | |
| 299 | + [ 'background' => Color::get_css( isset( $attributes['headerBackgroundColor'] ) ? $attributes['headerBackgroundColor'] : '' ) ], | |
| 300 | + isset( $attributes['headerBorder'] ) ? Border::get_css( $attributes['headerBorder'], '', $device ) : [], | |
| 301 | + isset( $attributes['headerPadding'] ) ? Dimensions::get_css( $attributes['headerPadding'], 'padding', $device ) : [] | |
| 302 | + ); | |
| 303 | + } | |
| 304 | + public function get_panel_hover_css( $attributes, $device = '' ) { | |
| 305 | + return array_merge( | |
| 306 | + [ 'background' => Color::get_css( isset( $attributes['headerBackgroundColorH'] ) ? $attributes['headerBackgroundColorH'] : '' ) ], | |
| 307 | + isset( $attributes['headerBorder'] ) ? Border::get_hover_css( $attributes['headerBorder'], '', $device ) : [] | |
| 308 | + ); | |
| 309 | + } | |
| 310 | + public function get_panel_active_css( $attributes, $device = '' ) { | |
| 311 | + return [ 'background' => Color::get_css( isset( $attributes['headerBackgroundActiveColor'] ) ? $attributes['headerBackgroundActiveColor'] : '' ) ]; | |
| 312 | + } | |
| 313 | + public function get_icon_css( $attributes, $device = '' ) { | |
| 314 | + return array_merge( | |
| 315 | + [ 'fill' => Color::get_css( isset( $attributes['iconColor'] ) ? $attributes['iconColor'] : '' ) ], | |
| 316 | + Range::get_css([ | |
| 317 | + 'attributeValue' => $attributes['iconSize'], | |
| 318 | + 'attribute_object_key' => 'value', | |
| 319 | + 'isResponsive' => false, | |
| 320 | + 'defaultValue' => 30, | |
| 321 | + 'property' => 'font-size', | |
| 322 | + 'device' => $device, | |
| 323 | + ]), | |
| 324 | + ); | |
| 325 | + } | |
| 326 | + public function get_icon_hover_css( $attributes ) { | |
| 327 | + return [ 'fill' => Color::get_css( isset( $attributes['iconColorH'] ) ? $attributes['iconColorH'] : '' ) ]; | |
| 328 | + } | |
| 329 | + public function get_icon_active_css( $attributes ) { | |
| 330 | + return [ 'fill' => Color::get_css( isset( $attributes['iconActiveColor'] ) ? $attributes['iconActiveColor'] : '' ) ]; | |
| 331 | + } | |
| 332 | + public function get_content_css( $attributes, $device = '' ) { | |
| 333 | + return array_merge( | |
| 334 | + [ 'background' => Color::get_css( isset( $attributes['bodyBackground'] ) ? $attributes['bodyBackground'] : '' ) ], | |
| 335 | + isset( $attributes['bodyPadding'] ) ? Dimensions::get_css( $attributes['bodyPadding'], 'padding', $device ) : [] | |
| 336 | + ); | |
| 337 | + } | |
| 338 | + public function get_content_hover_css( $attributes, $device = '' ) { | |
| 339 | + | |
| 340 | + return [ 'background' => Color::get_css( isset( $attributes['bodyBackgroundH'] ) ? $attributes['bodyBackgroundH'] : '' ) ]; | |
| 341 | + | |
| 342 | + } | |
| 343 | +} | |