| 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 |
); |
| 79 |
$css_generator->add_class_styles( |
| 80 |
'{{WRAPPER}} .ablocks-block--single-accordion:hover', |
| 81 |
$this->get_item_hover_css( $attributes ), |
| 82 |
$this->get_item_hover_css( $attributes, 'Tablet' ), |
| 83 |
$this->get_item_hover_css( $attributes, 'Mobile' ) |
| 84 |
); |
| 85 |
$css_generator->add_class_styles( |
| 86 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading .ablocks-block-accordion-title', |
| 87 |
$this->get_title_css( $attributes ), |
| 88 |
$this->get_title_css( $attributes, 'Tablet' ), |
| 89 |
$this->get_title_css( $attributes, 'Mobile' ) |
| 90 |
); |
| 91 |
$css_generator->add_class_styles( |
| 92 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading:hover .ablocks-block-accordion-title', |
| 93 |
$this->get_title_hover_css( $attributes ), |
| 94 |
$this->get_title_hover_css( $attributes, 'Tablet' ), |
| 95 |
$this->get_title_hover_css( $attributes, 'Mobile' ) |
| 96 |
); |
| 97 |
$css_generator->add_class_styles( |
| 98 |
'{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block-accordion-title', |
| 99 |
$this->get_title_active_css( $attributes ), |
| 100 |
$this->get_title_active_css( $attributes, 'Tablet' ), |
| 101 |
$this->get_title_active_css( $attributes, 'Mobile' ) |
| 102 |
); |
| 103 |
$css_generator->add_class_styles( |
| 104 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading', |
| 105 |
$this->get_panel_css( $attributes ), |
| 106 |
$this->get_panel_css( $attributes, 'Tablet' ), |
| 107 |
$this->get_panel_css( $attributes, 'Mobile' ) |
| 108 |
); |
| 109 |
$css_generator->add_class_styles( |
| 110 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading:hover', |
| 111 |
$this->get_panel_hover_css( $attributes ), |
| 112 |
$this->get_panel_hover_css( $attributes, 'Tablet' ), |
| 113 |
$this->get_panel_hover_css( $attributes, 'Mobile' ), |
| 114 |
); |
| 115 |
$css_generator->add_class_styles( |
| 116 |
'{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block--single-accordion__heading', |
| 117 |
$this->get_panel_active_css( $attributes ), |
| 118 |
$this->get_panel_active_css( $attributes, 'Tablet' ), |
| 119 |
$this->get_panel_active_css( $attributes, 'Mobile' ) |
| 120 |
); |
| 121 |
$css_generator->add_class_styles( |
| 122 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading svg.ablocks-svg-icon', |
| 123 |
$this->get_icon_css( $attributes ) |
| 124 |
); |
| 125 |
$css_generator->add_class_styles( |
| 126 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading:hover svg.ablocks-svg-icon', |
| 127 |
$this->get_icon_hover_css( $attributes ) |
| 128 |
); |
| 129 |
$css_generator->add_class_styles( |
| 130 |
'{{WRAPPER}} .ablocks-block--single-accordion-is-selected svg.ablocks-svg-icon', |
| 131 |
$this->get_icon_active_css( $attributes ) |
| 132 |
); |
| 133 |
$css_generator->add_class_styles( |
| 134 |
'{{WRAPPER}} .ablocks-block--single-accordion__body-content', |
| 135 |
$this->get_content_css( $attributes ), |
| 136 |
$this->get_content_css( $attributes, 'Tablet' ), |
| 137 |
$this->get_content_css( $attributes, 'Mobile' ) |
| 138 |
); |
| 139 |
$css_generator->add_class_styles( |
| 140 |
'{{WRAPPER}} .ablocks-block--single-accordion__body-content:hover', |
| 141 |
$this->get_content_hover_css( $attributes ), |
| 142 |
$this->get_content_hover_css( $attributes, 'Tablet' ), |
| 143 |
$this->get_content_hover_css( $attributes, 'Mobile' ) |
| 144 |
); |
| 145 |
return $css_generator->generate_css(); |
| 146 |
} |
| 147 |
public function build_css_v2( $attributes ) { |
| 148 |
$css_generator = new CssGeneratorV2( $attributes, $this->block_name ); |
| 149 |
|
| 150 |
$css_generator->add_class_styles( |
| 151 |
'{{WRAPPER}} .ablocks-block--single-accordion', |
| 152 |
$this->get_item_css( $attributes ), |
| 153 |
$this->get_item_css( $attributes, 'Tablet' ), |
| 154 |
$this->get_item_css( $attributes, 'Mobile' ) |
| 155 |
); |
| 156 |
$css_generator->add_class_styles( |
| 157 |
'{{WRAPPER}} .ablocks-block--single-accordion:hover', |
| 158 |
$this->get_item_hover_css( $attributes ), |
| 159 |
$this->get_item_hover_css( $attributes, 'Tablet' ), |
| 160 |
$this->get_item_hover_css( $attributes, 'Mobile' ) |
| 161 |
); |
| 162 |
$css_generator->add_class_styles( |
| 163 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading .ablocks-block-accordion-title', |
| 164 |
$this->get_title_css( $attributes ), |
| 165 |
$this->get_title_css( $attributes, 'Tablet' ), |
| 166 |
$this->get_title_css( $attributes, 'Mobile' ) |
| 167 |
); |
| 168 |
$css_generator->add_class_styles( |
| 169 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading:hover .ablocks-block-accordion-title', |
| 170 |
$this->get_title_hover_css( $attributes ), |
| 171 |
$this->get_title_hover_css( $attributes, 'Tablet' ), |
| 172 |
$this->get_title_hover_css( $attributes, 'Mobile' ) |
| 173 |
); |
| 174 |
$css_generator->add_class_styles( |
| 175 |
'{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block-accordion-title', |
| 176 |
$this->get_title_active_css( $attributes ), |
| 177 |
$this->get_title_active_css( $attributes, 'Tablet' ), |
| 178 |
$this->get_title_active_css( $attributes, 'Mobile' ) |
| 179 |
); |
| 180 |
$css_generator->add_class_styles( |
| 181 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading', |
| 182 |
$this->get_panel_css( $attributes ), |
| 183 |
$this->get_panel_css( $attributes, 'Tablet' ), |
| 184 |
$this->get_panel_css( $attributes, 'Mobile' ) |
| 185 |
); |
| 186 |
$css_generator->add_class_styles( |
| 187 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading:hover', |
| 188 |
$this->get_panel_hover_css( $attributes ), |
| 189 |
$this->get_panel_hover_css( $attributes, 'Tablet' ), |
| 190 |
$this->get_panel_hover_css( $attributes, 'Mobile' ), |
| 191 |
); |
| 192 |
$css_generator->add_class_styles( |
| 193 |
'{{WRAPPER}} .ablocks-block--single-accordion-is-selected .ablocks-block--single-accordion__heading', |
| 194 |
$this->get_panel_active_css( $attributes ), |
| 195 |
$this->get_panel_active_css( $attributes, 'Tablet' ), |
| 196 |
$this->get_panel_active_css( $attributes, 'Mobile' ) |
| 197 |
); |
| 198 |
$css_generator->add_class_styles( |
| 199 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading svg.ablocks-svg-icon', |
| 200 |
$this->get_icon_css( $attributes ) |
| 201 |
); |
| 202 |
$css_generator->add_class_styles( |
| 203 |
'{{WRAPPER}} .ablocks-block--single-accordion__heading:hover svg.ablocks-svg-icon', |
| 204 |
$this->get_icon_hover_css( $attributes ) |
| 205 |
); |
| 206 |
$css_generator->add_class_styles( |
| 207 |
'{{WRAPPER}} .ablocks-block--single-accordion-is-selected svg.ablocks-svg-icon', |
| 208 |
$this->get_icon_active_css( $attributes ) |
| 209 |
); |
| 210 |
$css_generator->add_class_styles( |
| 211 |
'{{WRAPPER}} .ablocks-block--single-accordion__body-content', |
| 212 |
$this->get_content_css( $attributes ), |
| 213 |
$this->get_content_css( $attributes, 'Tablet' ), |
| 214 |
$this->get_content_css( $attributes, 'Mobile' ) |
| 215 |
); |
| 216 |
$css_generator->add_class_styles( |
| 217 |
'{{WRAPPER}} .ablocks-block--single-accordion__body-content:hover', |
| 218 |
$this->get_content_hover_css( $attributes ), |
| 219 |
$this->get_content_hover_css( $attributes, 'Tablet' ), |
| 220 |
$this->get_content_hover_css( $attributes, 'Mobile' ) |
| 221 |
); |
| 222 |
return $css_generator->generate_css(); |
| 223 |
} |
| 224 |
public function build_css( $attributes ) { |
| 225 |
if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { |
| 226 |
return $this->build_css_v2( $attributes ); |
| 227 |
} |
| 228 |
return $this->build_css_v1( $attributes ); |
| 229 |
} |
| 230 |
public function get_item_css( $attributes, $device = '' ) { |
| 231 |
$css = []; |
| 232 |
|
| 233 |
return array_merge( |
| 234 |
Range::get_css([ |
| 235 |
'attributeValue' => $attributes['itemSpace'], |
| 236 |
'attribute_object_key' => 'value', |
| 237 |
'isResponsive' => false, |
| 238 |
'defaultValue' => 10, |
| 239 |
'property' => 'margin-bottom', |
| 240 |
'hasUnit' => false, |
| 241 |
'unitDefaultValue' => 'px', |
| 242 |
'device' => $device, |
| 243 |
|
| 244 |
]), |
| 245 |
$css, |
| 246 |
isset( $attributes['itemBorder'] ) ? Border::get_css( $attributes['itemBorder'], '', $device ) : [] |
| 247 |
); |
| 248 |
} |
| 249 |
public function get_item_hover_css( $attributes, $device = '' ) { |
| 250 |
return array_merge( |
| 251 |
isset( $attributes['itemBorder'] ) ? Border::get_hover_css( $attributes['itemBorder'], '', $device ) : [] |
| 252 |
); |
| 253 |
} |
| 254 |
public function get_title_css( $attributes, $device = '' ) { |
| 255 |
$typographyValueGlobal = ! empty( $attributes['headerTypographyGlobal'] ) ? $attributes['headerTypographyGlobal'] : ''; |
| 256 |
$typography_value = isset( $attributes['headerTypography'] ) ? $attributes['headerTypography'] : []; |
| 257 |
return array_merge( |
| 258 |
[ 'color' => Color::get_css( isset( $attributes['headerTextColor'] ) ? $attributes['headerTextColor'] : '' ) ], |
| 259 |
Range::get_css([ |
| 260 |
'attributeValue' => $attributes['iconSpace'], |
| 261 |
'attribute_object_key' => 'value', |
| 262 |
'isResponsive' => false, |
| 263 |
'defaultValue' => 10, |
| 264 |
'property' => 'margin-left', |
| 265 |
'device' => $device, |
| 266 |
]), |
| 267 |
Typography::get_css( $typography_value, '', $device, $typographyValueGlobal ), |
| 268 |
TextShadow::get_css( $attributes['headerTextShadow'], '', $device ), |
| 269 |
TextStroke::get_css( $attributes['headerTextStroke'], '', $device ) |
| 270 |
); |
| 271 |
} |
| 272 |
public function get_title_hover_css( $attributes, $device = '' ) { |
| 273 |
return [ 'color' => Color::get_css( isset( $attributes['headerTextColorH'] ) ? $attributes['headerTextColorH'] : '' ) ]; |
| 274 |
} |
| 275 |
public function get_title_active_css( $attributes, $device = '' ) { |
| 276 |
return [ 'color' => Color::get_css( isset( $attributes['headerTextActiveColor'] ) ? $attributes['headerTextActiveColor'] : '' ) ]; |
| 277 |
} |
| 278 |
public function get_panel_css( $attributes, $device = '' ) { |
| 279 |
return array_merge( |
| 280 |
[ 'background' => Color::get_css( isset( $attributes['headerBackgroundColor'] ) ? $attributes['headerBackgroundColor'] : '' ) ], |
| 281 |
isset( $attributes['headerBorder'] ) ? Border::get_css( $attributes['headerBorder'], '', $device ) : [], |
| 282 |
isset( $attributes['headerPadding'] ) ? Dimensions::get_css( $attributes['headerPadding'], 'padding', $device ) : [] |
| 283 |
); |
| 284 |
} |
| 285 |
public function get_panel_hover_css( $attributes, $device = '' ) { |
| 286 |
return array_merge( |
| 287 |
[ 'background' => Color::get_css( isset( $attributes['headerBackgroundColorH'] ) ? $attributes['headerBackgroundColorH'] : '' ) ], |
| 288 |
isset( $attributes['headerBorder'] ) ? Border::get_hover_css( $attributes['headerBorder'], '', $device ) : [] |
| 289 |
); |
| 290 |
} |
| 291 |
public function get_panel_active_css( $attributes, $device = '' ) { |
| 292 |
return [ 'background' => Color::get_css( isset( $attributes['headerBackgroundActiveColor'] ) ? $attributes['headerBackgroundActiveColor'] : '' ) ]; |
| 293 |
} |
| 294 |
public function get_icon_css( $attributes, $device = '' ) { |
| 295 |
return array_merge( |
| 296 |
[ 'fill' => Color::get_css( isset( $attributes['iconColor'] ) ? $attributes['iconColor'] : '' ) ], |
| 297 |
Range::get_css([ |
| 298 |
'attributeValue' => $attributes['iconSize'], |
| 299 |
'attribute_object_key' => 'value', |
| 300 |
'isResponsive' => false, |
| 301 |
'defaultValue' => 30, |
| 302 |
'property' => 'font-size', |
| 303 |
'device' => $device, |
| 304 |
]), |
| 305 |
); |
| 306 |
} |
| 307 |
public function get_icon_hover_css( $attributes ) { |
| 308 |
return [ 'fill' => Color::get_css( isset( $attributes['iconColorH'] ) ? $attributes['iconColorH'] : '' ) ]; |
| 309 |
} |
| 310 |
public function get_icon_active_css( $attributes ) { |
| 311 |
return [ 'fill' => Color::get_css( isset( $attributes['iconActiveColor'] ) ? $attributes['iconActiveColor'] : '' ) ]; |
| 312 |
} |
| 313 |
public function get_content_css( $attributes, $device = '' ) { |
| 314 |
return array_merge( |
| 315 |
[ 'background' => Color::get_css( isset( $attributes['bodyBackground'] ) ? $attributes['bodyBackground'] : '' ) ], |
| 316 |
isset( $attributes['bodyPadding'] ) ? Dimensions::get_css( $attributes['bodyPadding'], 'padding', $device ) : [] |
| 317 |
); |
| 318 |
} |
| 319 |
public function get_content_hover_css( $attributes, $device = '' ) { |
| 320 |
|
| 321 |
return [ 'background' => Color::get_css( isset( $attributes['bodyBackgroundH'] ) ? $attributes['bodyBackgroundH'] : '' ) ]; |
| 322 |
|
| 323 |
} |
| 324 |
} |
| 325 |
|