| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class AdvancedHeading extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/advanced-heading'; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
|
| 11 |
parent::__construct( self::$blockName ); |
| 12 |
|
| 13 |
register_block_type( |
| 14 |
'getwid/advanced-heading', |
| 15 |
array( |
| 16 |
'render_callback' => [ $this, 'render_callback' ] |
| 17 |
) |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
public function getLabel() { |
| 22 |
return __('Advanced Heading', 'getwid'); |
| 23 |
} |
| 24 |
|
| 25 |
public function block_frontend_assets() { |
| 26 |
|
| 27 |
if ( is_admin() ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
add_filter( 'getwid/optimize/assets', |
| 36 |
function ( $assets ) { |
| 37 |
$assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; |
| 38 |
|
| 39 |
return $assets; |
| 40 |
} |
| 41 |
); |
| 42 |
|
| 43 |
add_filter( 'getwid/optimize/should_load_common_css', '__return_true' ); |
| 44 |
|
| 45 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 46 |
|
| 47 |
wp_enqueue_style( |
| 48 |
self::$blockName, |
| 49 |
getwid_get_plugin_url( 'assets/blocks/advanced-heading/style' . $rtl . '.css' ), |
| 50 |
[], |
| 51 |
getwid()->settings()->getVersion() |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
public function render_callback( $attributes, $content ) { |
| 56 |
|
| 57 |
if ( isset( $attributes['fontWeight'] ) && |
| 58 |
( $attributes['fontWeight'] == 'regular' || $attributes['fontWeight'] == 'normal') ) { |
| 59 |
|
| 60 |
$attributes['fontWeight'] = '400'; |
| 61 |
} |
| 62 |
|
| 63 |
$should_load_gf = $this->shouldLoadGoogleFont( $attributes ); |
| 64 |
|
| 65 |
if ( $should_load_gf ) { |
| 66 |
|
| 67 |
$fontFamily = $attributes['fontFamily']; |
| 68 |
|
| 69 |
$fontFamilyHandle = strtolower( preg_replace( '/\s+/', '_', $fontFamily ) ); |
| 70 |
|
| 71 |
$fontWeight = ''; |
| 72 |
$fontWeightHandle = ''; |
| 73 |
$fontWeightPart = ''; |
| 74 |
if ( isset( $attributes['fontWeight'] ) && $attributes['fontWeight'] != '400' ) { |
| 75 |
$fontWeight = $attributes['fontWeight']; |
| 76 |
$fontWeightHandle = '_' . $fontWeight; |
| 77 |
$fontWeightPart = ':' . $fontWeight; |
| 78 |
} |
| 79 |
|
| 80 |
wp_enqueue_style( |
| 81 |
'google-font-' . esc_attr( $fontFamilyHandle ) . esc_attr( $fontWeightHandle ), |
| 82 |
'https://fonts.googleapis.com/css?family=' . esc_attr( $fontFamily ) . esc_attr( $fontWeightPart ), |
| 83 |
null, |
| 84 |
'all' |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
$this->block_frontend_assets(); |
| 89 |
|
| 90 |
return $content; |
| 91 |
} |
| 92 |
|
| 93 |
private function shouldLoadGoogleFont( $attributes ) { |
| 94 |
$should_load = false; |
| 95 |
|
| 96 |
// if fontFamily set maybe GF should be loaded |
| 97 |
if ( isset( $attributes['fontFamily'] ) && !empty( $attributes['fontFamily'] ) ) { |
| 98 |
$should_load = true; |
| 99 |
} |
| 100 |
|
| 101 |
// if fontGroupID isset but not equal to 'google-fonts' it shouldn't be loaded |
| 102 |
// if fontGroupID is not set(older plugin versions) the condition above will do all the work |
| 103 |
if ( $should_load && isset( $attributes['fontGroupID'] ) && $attributes['fontGroupID'] != 'google-fonts' ) { |
| 104 |
$should_load = false; |
| 105 |
} |
| 106 |
|
| 107 |
return $should_load; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
getwid()->blocksManager()->addBlock( |
| 112 |
new \Getwid\Blocks\AdvancedHeading() |
| 113 |
); |
| 114 |
|