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