PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
← All changes | includes/blocks/advanced-heading.php +78 -113 2.0.93.0.2 View file →
@@ -1,113 +1,78 @@
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 -);
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 +);