| @@ -1,0 +1,208 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\Heading; | |
| 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\Controls\Alignment; | |
| 12 | +use ABlocks\Controls\Typography; | |
| 13 | +use ABlocks\Controls\TextShadow; | |
| 14 | +use ABlocks\Controls\TextStroke; | |
| 15 | +use ABlocks\Controls\Color; | |
| 16 | + | |
| 17 | +class Block extends BlockBaseAbstract { | |
| 18 | + protected $block_name = 'heading'; | |
| 19 | + | |
| 20 | + public function build_css_v1( $attributes ) { | |
| 21 | + $css_generator = new CssGenerator( $attributes, $this->block_name ); | |
| 22 | + $css_generator->add_class_styles( | |
| 23 | + '{{WRAPPER}}', | |
| 24 | + $this->get_wrapper_css( $attributes ), | |
| 25 | + $this->get_wrapper_css( $attributes, 'Tablet' ), | |
| 26 | + $this->get_wrapper_css( $attributes, 'Mobile' ) | |
| 27 | + ); | |
| 28 | + | |
| 29 | + $css_generator->add_class_styles( | |
| 30 | + '{{WRAPPER}} .ablocks-heading-text', | |
| 31 | + $this->get_heading_text_css( $attributes ), | |
| 32 | + $this->get_heading_text_css( $attributes, 'Tablet' ), | |
| 33 | + $this->get_heading_text_css( $attributes, 'Mobile' ) | |
| 34 | + ); | |
| 35 | + | |
| 36 | + $desktop_heading_a_styles = $this->get_heading_a_css( $attributes ); | |
| 37 | + $css_generator->add_class_styles( | |
| 38 | + '{{WRAPPER}}.ablocks-block--heading a', | |
| 39 | + $desktop_heading_a_styles, | |
| 40 | + ); | |
| 41 | + | |
| 42 | + $css_generator->add_class_styles( | |
| 43 | + '{{WRAPPER}} .ablocks-animated-text path', | |
| 44 | + $this->get_svg_path_css( $attributes ), | |
| 45 | + $this->get_svg_path_css( $attributes, 'Tablet' ), | |
| 46 | + $this->get_svg_path_css( $attributes, 'Mobile' ) | |
| 47 | + ); | |
| 48 | + | |
| 49 | + $css_generator->add_class_styles( | |
| 50 | + '{{WRAPPER}} .ablocks-animated-text', | |
| 51 | + $this->get_heading_general_css( $attributes ), | |
| 52 | + $this->get_heading_general_css( $attributes, 'Tablet' ), | |
| 53 | + $this->get_heading_general_css( $attributes, 'Mobile' ) | |
| 54 | + ); | |
| 55 | + | |
| 56 | + $css_generator->add_class_styles( | |
| 57 | + '{{WRAPPER}} .ablocks-animated-text-wrapper', | |
| 58 | + $this->get_heading_animated_css( $attributes ), | |
| 59 | + $this->get_heading_animated_css( $attributes, 'Tablet' ), | |
| 60 | + $this->get_heading_animated_css( $attributes, 'Mobile' ) | |
| 61 | + ); | |
| 62 | + | |
| 63 | + return $css_generator->generate_css(); | |
| 64 | + } | |
| 65 | + public function build_css_v2( $attributes ) { | |
| 66 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 67 | + $css_generator->add_class_styles( | |
| 68 | + '{{WRAPPER}}', | |
| 69 | + $this->get_wrapper_css( $attributes ), | |
| 70 | + $this->get_wrapper_css( $attributes, 'Tablet' ), | |
| 71 | + $this->get_wrapper_css( $attributes, 'Mobile' ) | |
| 72 | + ); | |
| 73 | + | |
| 74 | + $css_generator->add_class_styles( | |
| 75 | + '{{WRAPPER}} .ablocks-heading-text', | |
| 76 | + $this->get_heading_text_css( $attributes ), | |
| 77 | + $this->get_heading_text_css( $attributes, 'Tablet' ), | |
| 78 | + $this->get_heading_text_css( $attributes, 'Mobile' ) | |
| 79 | + ); | |
| 80 | + $desktop_heading_a_styles = $this->get_heading_a_css( $attributes ); | |
| 81 | + $css_generator->add_class_styles( | |
| 82 | + '{{WRAPPER}}.ablocks-block--heading a', | |
| 83 | + $desktop_heading_a_styles, | |
| 84 | + ); | |
| 85 | + if ( ! empty( $attributes['isAnimated'] ) ) { | |
| 86 | + $css_generator->add_class_styles( | |
| 87 | + '{{WRAPPER}} .ablocks-animated-text path', | |
| 88 | + $this->get_svg_path_css( $attributes ), | |
| 89 | + $this->get_svg_path_css( $attributes, 'Tablet' ), | |
| 90 | + $this->get_svg_path_css( $attributes, 'Mobile' ) | |
| 91 | + ); | |
| 92 | + | |
| 93 | + $css_generator->add_class_styles( | |
| 94 | + '{{WRAPPER}} .ablocks-animated-text', | |
| 95 | + $this->get_heading_general_css( $attributes ), | |
| 96 | + $this->get_heading_general_css( $attributes, 'Tablet' ), | |
| 97 | + $this->get_heading_general_css( $attributes, 'Mobile' ) | |
| 98 | + ); | |
| 99 | + | |
| 100 | + $css_generator->add_class_styles( | |
| 101 | + '{{WRAPPER}} .ablocks-animated-text-wrapper', | |
| 102 | + $this->get_heading_animated_css( $attributes ), | |
| 103 | + $this->get_heading_animated_css( $attributes, 'Tablet' ), | |
| 104 | + $this->get_heading_animated_css( $attributes, 'Mobile' ) | |
| 105 | + ); | |
| 106 | + }//end if | |
| 107 | + | |
| 108 | + return $css_generator->generate_css(); | |
| 109 | + } | |
| 110 | + | |
| 111 | + public function build_css( $attributes ) { | |
| 112 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 113 | + return $this->build_css_v2( $attributes ); | |
| 114 | + } | |
| 115 | + return $this->build_css_v1( $attributes ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + public function get_wrapper_css( $attributes, $device = '' ) { | |
| 119 | + return isset( $attributes['alignment'] ) ? Alignment::get_css( $attributes['alignment'], 'text-align', $device ) : []; | |
| 120 | + } | |
| 121 | + public function get_heading_text_css( $attributes, $device = '' ) { | |
| 122 | + $typographyValueGlobal = ! empty( $attributes['typographyGlobal'] ) ? $attributes['typographyGlobal'] : ''; | |
| 123 | + $typography_css = ! empty( $attributes['typography'] ) ? Typography::get_css( $attributes['typography'], '', $device, $typographyValueGlobal ) : array(); | |
| 124 | + $textShadowCss = ! empty( $attributes['textShadow'] ) ? TextShadow::get_css( $attributes['textShadow'], '', $device ) : array(); | |
| 125 | + $textStrokeCss = ! empty( $attributes['textStroke'] ) ? TextStroke::get_css( $attributes['textStroke'], '', $device ) : array(); | |
| 126 | + | |
| 127 | + return array_merge( [ | |
| 128 | + 'color' => Color::get_css( isset( $attributes['textColor'] ) ? $attributes['textColor'] : '' ), | |
| 129 | + ], $typography_css, $textShadowCss, $textStrokeCss ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + public function get_heading_a_css( $attributes ) { | |
| 133 | + $decoration = isset( $attributes ['typography']['decoration'] ) ? $attributes['typography'] ['decoration'] : ''; | |
| 134 | + if ( ! empty( $decoration ) ) { | |
| 135 | + return array( 'text-decoration' => $decoration ); } | |
| 136 | + return array(); | |
| 137 | + } | |
| 138 | + | |
| 139 | + public function render_block_content( $attributes, $content, $block_instance ) { | |
| 140 | + if ( ! isset( $block_instance->context['postId'] ) ) { | |
| 141 | + return ''; | |
| 142 | + } | |
| 143 | + $title = get_the_title(); | |
| 144 | + if ( ! $title ) { | |
| 145 | + return ''; | |
| 146 | + } | |
| 147 | + $tag_name = 'h2'; | |
| 148 | + if ( ! empty( $attributes['headingTag'] ) ) { | |
| 149 | + $tag_name = $attributes['headingTag']; | |
| 150 | + } | |
| 151 | + return sprintf( | |
| 152 | + '<%1$s>%2$s</%1$s>', | |
| 153 | + $tag_name, | |
| 154 | + $title | |
| 155 | + ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + | |
| 159 | + public function get_svg_path_css( $attributes, $device = '' ) { | |
| 160 | + $count = ! empty( $attributes['isInfiniteLoop'] ) ? 'infinite' : 1; | |
| 161 | + | |
| 162 | + $highlightStrokeWidth = isset( $attributes['highlightStrokeWidth'] ) && is_array( $attributes['highlightStrokeWidth'] ) ? $attributes['highlightStrokeWidth'] : []; | |
| 163 | + $unit = $highlightStrokeWidth[ 'valueUnit' . $device ] ?? $highlightStrokeWidth['valueUnit'] ?? 'px'; | |
| 164 | + $value = $highlightStrokeWidth[ 'value' . $device ] ?? $highlightStrokeWidth['value'] ?? 10; | |
| 165 | + $stroke_width = $value . $unit; | |
| 166 | + $highlightDuration = isset( $attributes['highlightDuration'] ) ? $attributes['highlightDuration'] : 0; | |
| 167 | + $highlightDelay = isset( $attributes['highlightDelay'] ) ? $attributes['highlightDelay'] : 0; | |
| 168 | + | |
| 169 | + return array_merge( | |
| 170 | + [ | |
| 171 | + 'stroke' => Color::get_css( | |
| 172 | + isset( $attributes['highlightColor'] ) | |
| 173 | + ? $attributes['highlightColor'] | |
| 174 | + : '#000' | |
| 175 | + ) | |
| 176 | + ], | |
| 177 | + [ | |
| 178 | + 'stroke-width' => $stroke_width, | |
| 179 | + 'animation' => 'draw-auto ' . ( $highlightDuration + $highlightDelay ) . 'ms forwards ' . $count | |
| 180 | + ] | |
| 181 | + ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + public function get_heading_general_css( $attributes, $device = '' ) { | |
| 187 | + $typographyValueGlobal = ! empty( $attributes['typographyGlobal'] ) ? $attributes['typographyGlobal'] : ''; | |
| 188 | + $typography_value = isset( $attributes['typography'] ) ? $attributes['typography'] : []; | |
| 189 | + $stock_unit_value = isset( $attributes['textStroke'] ) ? $attributes['textStroke'] : []; | |
| 190 | + return array_merge( | |
| 191 | + [ 'color' => Color::get_css( isset( $attributes['textColor'] ) ? $attributes['textColor'] : '' ) ], | |
| 192 | + Typography::get_css( $typography_value, '', $device, $typographyValueGlobal ), | |
| 193 | + TextStroke::get_css( $stock_unit_value, '', $device ), | |
| 194 | + TextShadow::get_css( isset( $attributes['textShadow'] ) ? $attributes['textShadow'] : [], '', $device ) | |
| 195 | + ); | |
| 196 | + } | |
| 197 | + | |
| 198 | + public function get_heading_animated_css( $attributes, $device = '' ) { | |
| 199 | + $typography_value = isset( $attributes['animatedTypography'] ) ? $attributes['animatedTypography'] : []; | |
| 200 | + $stroke_unit_value = isset( $attributes['animatedTextStroke'] ) ? $attributes['animatedTextStroke'] : []; | |
| 201 | + return array_merge( | |
| 202 | + [ 'color' => Color::get_css( isset( $attributes['animatedTextColor'] ) ? $attributes['animatedTextColor'] : '' ) ], | |
| 203 | + Typography::get_css( $typography_value, '', $device ), | |
| 204 | + TextStroke::get_css( $stroke_unit_value, '', $device ), | |
| 205 | + TextShadow::get_css( isset( $attributes['animatedTextShadow'] ) ? $attributes['animatedTextShadow'] : [], '', $device ) | |
| 206 | + ); | |
| 207 | + } | |
| 208 | +} | |