| @@ -1,107 +1,72 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Getwid\Blocks; |
| 4 | 4 | |
| 5 | -class AdvancedHeading extends \Getwid\Blocks\AbstractBlock { | |
| 5 | +class AdvancedHeading extends AbstractBlock { | |
| 6 | 6 | |
| 7 | - protected static $blockName = 'getwid/advanced-heading'; | |
| 7 | + public function __construct() { | |
| 8 | 8 | |
| 9 | - public function __construct() { | |
| 9 | + parent::__construct( 'getwid/advanced-heading' ); | |
| 10 | 10 | |
| 11 | - parent::__construct( self::$blockName ); | |
| 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 | + } | |
| 12 | 18 | |
| 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'); | |
| 19 | + public function get_label() { | |
| 20 | + return __( 'Advanced Heading', 'getwid' ); | |
| 23 | 21 | } |
| 24 | 22 | |
| 25 | - public function block_frontend_assets() { | |
| 23 | + public function render_callback( $attributes, $content ) { | |
| 26 | 24 | |
| 27 | - if ( is_admin() ) { | |
| 28 | - return; | |
| 29 | - } | |
| 30 | - | |
| 31 | - if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { | |
| 32 | - return; | |
| 25 | + if ( | |
| 26 | + isset( $attributes['fontWeight'] ) && | |
| 27 | + ( 'regular' === $attributes['fontWeight'] || 'normal' === $attributes['fontWeight'] ) | |
| 28 | + ) { | |
| 29 | + $attributes['fontWeight'] = '400'; | |
| 33 | 30 | } |
| 34 | 31 | |
| 35 | - add_filter( 'getwid/optimize/assets', | |
| 36 | - function ( $assets ) { | |
| 37 | - $assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; | |
| 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 | 38 | |
| 39 | - return $assets; | |
| 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; | |
| 40 | 43 | } |
| 41 | - ); | |
| 42 | 44 | |
| 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 | 45 | 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 ), | |
| 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 ), | |
| 83 | 48 | null, |
| 84 | - 'all' | |
| 49 | + getwid()->settings()->getVersion() | |
| 85 | 50 | ); |
| 86 | 51 | } |
| 87 | 52 | |
| 88 | - $this->block_frontend_assets(); | |
| 53 | + return $content; | |
| 54 | + } | |
| 89 | 55 | |
| 90 | - return $content; | |
| 91 | - } | |
| 56 | + private function should_load_google_font( $attributes ) { | |
| 92 | 57 | |
| 93 | - private function shouldLoadGoogleFont( $attributes ) { | |
| 94 | - $should_load = false; | |
| 58 | + $should_load = false; | |
| 95 | 59 | |
| 96 | - // if fontFamily set maybe GF should be loaded | |
| 97 | - if ( isset( $attributes['fontFamily'] ) && !empty( $attributes['fontFamily'] ) ) { | |
| 60 | + if ( isset( $attributes['fontFamily'] ) && ! empty( $attributes['fontFamily'] ) ) { | |
| 98 | 61 | $should_load = true; |
| 99 | 62 | } |
| 100 | 63 | |
| 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' ) { | |
| 64 | + if ( | |
| 65 | + $should_load && | |
| 66 | + isset( $attributes['fontGroupID'] ) && | |
| 67 | + 'google-fonts' !== $attributes['fontGroupID'] | |
| 68 | + ) { | |
| 104 | 69 | $should_load = false; |
| 105 | 70 | } |
| 106 | 71 | |
| 107 | 72 | return $should_load; |
| @@ -108,6 +73,6 @@ | ||
| 108 | 73 | } |
| 109 | 74 | } |
| 110 | 75 | |
| 111 | 76 | getwid()->blocksManager()->addBlock( |
| 112 | - new \Getwid\Blocks\AdvancedHeading() | |
| 77 | + new AdvancedHeading() | |
| 113 | 78 | ); |