| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class AdvancedHeading extends AbstractBlock { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
|
| 9 |
parent::__construct( 'getwid/advanced-heading' ); |
| 10 |
|
| 11 |
register_block_type( |
| 12 |
getwid_get_plugin_path( 'assets/blocks/advanced-heading' ), |
| 13 |
array( |
| 14 |
'render_callback' => array( $this, 'render_callback' ), |
| 15 |
) |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
public function get_label() { |
| 20 |
return __( 'Advanced Heading', 'getwid' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function render_callback( $attributes, $content ) { |
| 24 |
|
| 25 |
if ( |
| 26 |
isset( $attributes['fontWeight'] ) && |
| 27 |
( 'regular' === $attributes['fontWeight'] || 'normal' === $attributes['fontWeight'] ) |
| 28 |
) { |
| 29 |
$attributes['fontWeight'] = '400'; |
| 30 |
} |
| 31 |
|
| 32 |
if ( $this->should_load_google_font( $attributes ) ) { |
| 33 |
$font_family = $attributes['fontFamily']; |
| 34 |
$font_family_handle = strtolower( preg_replace( '/\s+/', '_', $font_family ) ); |
| 35 |
$font_weight = ''; |
| 36 |
$font_weight_handle = ''; |
| 37 |
$font_weight_part = ''; |
| 38 |
|
| 39 |
if ( isset( $attributes['fontWeight'] ) && '400' !== $attributes['fontWeight'] ) { |
| 40 |
$font_weight = $attributes['fontWeight']; |
| 41 |
$font_weight_handle = '_' . $font_weight; |
| 42 |
$font_weight_part = ':' . $font_weight; |
| 43 |
} |
| 44 |
|
| 45 |
wp_enqueue_style( |
| 46 |
'google-font-' . esc_attr( $font_family_handle ) . esc_attr( $font_weight_handle ), |
| 47 |
'https://fonts.googleapis.com/css?family=' . esc_attr( $font_family ) . esc_attr( $font_weight_part ), |
| 48 |
null, |
| 49 |
getwid()->settings()->getVersion() |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
return $content; |
| 54 |
} |
| 55 |
|
| 56 |
private function should_load_google_font( $attributes ) { |
| 57 |
|
| 58 |
$should_load = false; |
| 59 |
|
| 60 |
if ( isset( $attributes['fontFamily'] ) && ! empty( $attributes['fontFamily'] ) ) { |
| 61 |
$should_load = true; |
| 62 |
} |
| 63 |
|
| 64 |
if ( |
| 65 |
$should_load && |
| 66 |
isset( $attributes['fontGroupID'] ) && |
| 67 |
'google-fonts' !== $attributes['fontGroupID'] |
| 68 |
) { |
| 69 |
$should_load = false; |
| 70 |
} |
| 71 |
|
| 72 |
return $should_load; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
getwid()->blocksManager()->addBlock( |
| 77 |
new AdvancedHeading() |
| 78 |
); |
| 79 |
|